Spring Mvc method Annotation Blocker

Source: Internet
Author: User

The application scenario, at the method level to authenticate the call, such as the API interface has a user uniquely labeled Accesstoken, for each request with Accesstoken can be added to the method of a interceptor, the user to obtain this request, stored in the request or Session field.

In python , the adorner can be used in Python flask to preprocess the method for permission handling

Take a look at an example and use @access_required to intercept:

@api. Route ('/post_apply ') @access_requireddef apply (): "" "," "" "" "" "" Print "is: ' +g.user return jsonify (res Ponse_data)

The implementation is simple:

#  Verify Access_token and save current user to G def access_required (f):     @wraps (f)      def decorated_func (*args, **kwargs):         access_ Token = request.values.get (' Access_token ')         if  Access_token == none:            return  error_msg (',  ' access_token required ')         if  access_token ==  "":             Return error_msg (',  ' access_token can not empty ')          if is_access_token (Access_token)  == False:             return error_msg (',  ' Invalid_access_token ')        &nbsP; return f (*args, **kwargs)     return decorated_func 


in Java , a custom annotation interceptor is implemented to add an annotation to the required interception method @accessrequired

Examples of usages in the Spring MVC controller

/** * Annotation Blocker method * @return */@RequestMapping (value= "/urlinter", Method=requestmethod.get) @AccessRequiredpublic @ Responsebody String urlinterceptortest () {return "via Interceptor: User" +request.getattribute ("Curruser");}

How to implement the above example?

Define an annotation:

Import Java.lang.annotation.elementtype;import Java.lang.annotation.retentionpolicy;import Java.lang.annotation.target;import java.lang.annotation.Retention; @Target (Elementtype.method) @Retention ( retentionpolicy.runtime) public @interface accessrequired {}

Engage an Interceptor:

 

In the spring MVC configuration file:

<!--Interceptor--><mvc:interceptors><mvc:interceptor><!--for all request interception using/**, for blocking requests under a module:/mypath/*-- ><mvc:mapping path= "/**"/><ref bean= "Useraccessinterceptor"/></MVC:INTERCEPTOR></MVC: Interceptors><bean id= "Useraccessinterceptor" class= " Com.banmacoffee.web.interceptor.UserAccessApiInterceptor "></bean>

When you're done, you can do whatever you want in the interceptor, and load it up any way you like the controller request.

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.