Recently, user permissions have been added to the project, and some operations require logon. Some operations are not required. Before that, project permissions have been implemented and filters are preferred. However, filters are rigid to be used here. If they are used, all the methods must be added to the configuration file, and Wildcards are not recommended. So I thought about it. A simple and flexible permission judgment that was previously used by everyone was completed using the methhodInterceptor interceptor of Spring and based on annotations.
Now I have written one set myself. The usage is like this:
@ LoginRequired
@ RequestMapping (value = "/comment ")
Public void comment (HttpServletRequest req, HttpServletResponse res ){
DoSomething ,,,,,,,,
}
I intercepted the method on the controller layer of Spring mvc. Note that the @ LoginRequired above is my custom annotation. In this case, if this annotation is provided after the method is intercepted, it indicates that the method requires the user to log on before performing some operation, we can determine whether the session or Cookie in the request contains the user's login identity, and then determine whether to execute this method; if not, execute another operation.
-------------------------------------------------------------------------
The code for custom annotation is as follows:
Package com. qunar. wireless. ugc. controllor. web;
Import java. lang. annotation. ElementType;
Import java. lang. annotation. Retention;
Import java. lang. annotation. RetentionPolicy;
Import java. lang. annotation. Target;
@ Target (ElementType. METHOD)
@ Retention (RetentionPolicy. RUNTIME)
Public @ interface LoginRequired {
}
-----------------------------------------------------------------------------
The following is a custom method interceptor that continues to use the MethodInterceptor of aop.
Import javax. servlet. http. HttpServletRequest;
Import org. aopalliance. intercept. MethodInterceptor;
Import org. aopalliance. intercept. MethodInvocation;
Import com. qunar. wireless. ugc. controllor. web. LoginRequired;
/**
* @ Author tao. zhang
* @ Create-time 2012-2-31
*/
Public class LoginRequiredInterceptor1 implements MethodInterceptor {
@ Override
Public Object invoke (MethodInvocation mi) throws Throwable {
Object [] ars = mi. getArguments ();
For (Object o: ars ){
If (o instanceof HttpServletRequest ){
System. out. println ("------------ this is a HttpServletRequest Parameter ------------");
}
}
// Determine whether the @ LoginRequired annotation is added to the Method
If (mi. getMethod (). isAnnotationPresent (LoginRequired. class )){
System. out. println ("---------- this method is added @ LoginRequired -------------------------");
}
// Execute the intercepted method. Remember that if this method is not called, The intercepted method will not be executed.
Return mi. proceed ();
}
}
------------------------------------------------------------------------
Configuration file:
<Bean id = "springMethodInterceptor" class = "com. qunar. wireless. ugc. interceptor. LoginRequiredInterceptor1"> </bean>
<Aop: config>
<! -- Entry point -->
<Aop: pointcut id = "loginPoint"
Expression = "execution (public * com. qunar. wireless. ugc. controllor. web. *. * (..)"/>
<! -- Use the custom interceptor at this entry point -->
<Aop: advisor pointcut-ref = "loginPoint" advice-ref = "springMethodInterceptor"/>
</Aop: config>
Author: gameover8080