Use custom annotations for login interception control in SPRINGMVC

Source: Internet
Author: User

1:java annotations are used quite frequently, especially when building frameworks where the reflection of a class is used to obtain methods and properties.

There are four meta-annotations in Java: @Retention @Target @Document @Inherited;

1 @Retention: Where annotations are held2@Retention (Retentionpolicy.source)//annotations exist only in the source code and are not included in the class bytecode file3@Retention (Retentionpolicy.class)//The default retention policy, annotations are present in the class bytecode file, but are not available at run time .4@Retention (Retentionpolicy.runtime)//annotations exist in the class bytecode file and can be retrieved at run time through reflection5         6 @Target: The purpose of the annotation7               8@Target (Elementtype.type)//interfaces, classes, enumerations, annotations9@Target (Elementtype.field)//constants for fields, enumerationsTen@Target (Elementtype.method)//Method One@Target (Elementtype.parameter)//Method Parameters A@Target (Elementtype.constructor)//constructor Function -@Target (elementtype.local_variable)//Local Variables -@Target (Elementtype.annotation_type)//Annotations the@Target (Elementtype.package)///Bag -       - @Document: Note that the note will be included in the Javadoc -       +@Inherited: The description subclass can inherit the annotation from the parent class

To create a custom annotation class:

1  PackageCom.liveyc.eloan.util;2 3 ImportJava.lang.annotation.ElementType;4 Importjava.lang.annotation.Retention;5 ImportJava.lang.annotation.RetentionPolicy;6 ImportJava.lang.annotation.Target;7 8 /**9 * Login Tag RequiredTen  * @authorAdministrator One  * A  */ - @Target (Elementtype.method) - @Retention (retentionpolicy.runtime) the  Public@InterfaceRequirelogin { -  -}

2: Write Interceptor in Java interception is passed down so to return false do not continue to pass down the

1  PackageCom.liveyc.eloan.base.interceptor;2 3 Importjavax.servlet.http.HttpServletRequest;4 ImportJavax.servlet.http.HttpServletResponse;5 6 ImportOrg.springframework.web.method.HandlerMethod;7 ImportOrg.springframework.web.servlet.handler.HandlerInterceptorAdapter;8 9 ImportCom.liveyc.eloan.util.RequireLogin;Ten ImportCom.liveyc.eloan.util.UserContext; One  A /** - * Login Blocker -  *  the  * @authorAdministrator -  *  -  */ -  Public classLogininterceptorextendsHandlerinterceptoradapter { +  - @Override +      Public BooleanPrehandle (httpservletrequest request, AHttpServletResponse response, Object handler)throwsException { at         //processing handler; -         if(HandlerinstanceofHandlermethod) { -             //determine if there is a label on the current method; -Handlermethod HM =(Handlermethod) handler; -             if(Hm.getmethodannotation (Requirelogin.class) !=NULL -&& usercontext.getcurrent () = =NULL) { in                 //R If there is, determine whether the user is currently logged in, if not logged in, jump to the login page -Response.sendredirect ("/login.html"); to                 return false; +             } -         } the         return Super. Prehandle (Request, response, handler); *     } $ Panax Notoginseng}

3: Add in Spring's XML configuration file

1     <mvc:interceptors>2         <!--Configuring the Login blocker -3         <Mvc:interceptor>4             <mvc:mappingPath="/**" />5             <Beanclass= "Com.liveyc.eloan.base.interceptor.LoginInterceptor" />6         </Mvc:interceptor>7     </mvc:interceptors>

4: Add custom Annotations to methods that SPRINGMVC's controller layer needs to log in to access later @RequireLogin

1 @RequireLogin2@RequestMapping ("Personal")3  PublicString Personal (model model) {4Logininfo current =usercontext.getcurrent ();5Model.addattribute ("UserInfo", This. Userinfoservice.get (Current.getid ()));6Model.addattribute ("Account", This. Accountservice.get (Current.getid ()));7     return"Personal";8}

5: Start Project start test

Because it is not logged in, it jumps directly to the login page.

Access a method page with no custom annotations error

Log in after page normal access

Use custom annotations for login interception control in SPRINGMVC

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.