Spring Login Verification Note distinguishes Login role

Source: Internet
Author: User

First, the Interceptor code.

import javax.annotation.resource;import javax.servlet.http.httpservletrequest;import  Javax.servlet.http.httpservletresponse;import org.slf4j.logger;import org.slf4j.loggerfactory;import  org.springframework.web.method.HandlerMethod;import  org.springframework.web.servlet.handler.handlerinterceptoradapter;import  com.tutor.normal.entity.tutoruser;import com.tutor.normal.service.itutoruserservice;import  com.tutor.normal.status.usertype;import com.tutor.tool.util.resultutil;public class  Tutorloginfilter extends handlerinterceptoradapter {    private static  logger log = loggerfactory.getlogger (Tutorloginfilter.class);          @Resource (name =  "Tutoruserservice")     private  itutoruserservice tutoruserservice;         @Override      publiC boolean prehandle (httpservletrequest request, httpservletresponse response,  Object handler)  throws exception {        log.info (Handler.getclass (). toString ());                 string token = request.getparameter ("token");                 TutorUser user = null;         try {             HandlerMethod method =  (Handlermethod)  handler;             TutorLoginAnnotation annotation =  Method.getmethodannotation (Tutorloginannotation.class);               &nbsP;         if (annotation == null)  {                 return true;             } else {                 user =  Tutoruserservice.validuser (Token, annotation.type (). GetValue ());                 log.info ("User user="  + user);             }                     } catch  (Exception e)  {            log.error (E.getMessage ());              response.setcontenttype ("Text/html;charset=utf-8");             response.getwriter (). Write (Resultutil.returnjson (e));             response.getwriter (). Flush ();             response.getwriter (). Close ();             return false;        }         return true;    }}

This is my note.

Package Com.tutor.normal.filter;import Java.lang.annotation.documented;import Java.lang.annotation.ElementType; Import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import Java.lang.annotation.target;import Com.tutor.normal.status.UserType; @Documented @retention ( Retentionpolicy.runtime) @Target ({Elementtype.method}) public @interface tutorloginannotation {public usertype type () Default Usertype.all;}

This is an enumeration.

Package com.tutor.normal.status;public enum usertype {    student ("Student") ,  1),  teacher ("Teacher",  2),  all ("All",  0);     private string  name;    private int value;    private usertype ( String name, int value)  {        this.name =  name;        this.value = value;     }    public string getname ()  {         return name;    }    public void setname ( String name)  {        this.name = name;     }    public int getvalue ()  {         return Value;    }    public void setvalue (Int value)  {         this.value = value;    }}

Annotation usage

@TutorLoginAnnotation (type = usertype.teacher) @RequestMapping ("/tea/search/{v}") @ResponseBody public Resultbean Myfun () {...}

Finally, the configuration of the Interceptor in spring

<mvc:interceptors> <mvc:interceptor> <mvc:mapping path= "/stu/**"/> <mvc : Mapping path= "/tch/**"/> <mvc:mapping path= "/ord/**"/> <bean class= "Com.tutor.normal. Filter. Tutorloginfilter "/> </mvc:interceptor> </mvc:interceptors>


Spring Login Verification Note distinguishes Login role

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.