Home >Web Front-end >JS Tutorial >springmvc implements annotation interceptor
This time I will bring you springmvc to implement the annotation interceptor. What are the precautions for springmvc to implement the annotation interceptor. The following is a practical case, let's take a look.
When I was writing a project with SpringMvc recently, I encountered a problem, which was the authentication problem of the method. After working on it for a day, I finally solved it. Let’s take a look at the solution.
Project Requirements: Where authentication is required, I only need to label it. For example, operations that can only be performed by user login. Generally, we will first perform the method when executing the method. Verifying the user's identity adds a lot of workload and reinvents the wheel. With java annotations, you only need to put a label on the method that requires authentication:
Solution :
1. First create an annotation class:
@Documented @Inherited @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Auth { boolean validate() default true; }
2. Create another interceptor:
public class AuthInterceptor extends BaseInterceptor{ @Override public Boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if(handler.getClass().isAssignableFrom(HandlerMethod.class)){ Auth authPassport = ((HandlerMethod) handler).getMethodAnnotation(Auth.class); //没有声明需要权限,或者声明不验证权限 if(authPassport==null){ return true; } else{ //在这里实现自己的权限验证逻辑 if(true){ //如果验证成功返回true(这里直接写false来模拟验证失败的处理) System.out.println("执行权限校验了"); return true; } else{ //如果验证失败 //返回到登录界面 // System.out.println("权限校验对了"); // response.sendRedirect("account/login"); return false; } } } else{ return true; } } }
3. Configure the interceptor: You need to add the following code to *-servlet.xml. If you have customized the configuration file, you can also put it directly into the configuration file you defined
<mvc:interceptors> <bean class="com.benxq.shop.user.interceptors.AuthInterceptor"/> </mvc:interceptors>
Note: You need to change the default to RequestMappingHandlerMapping and add the bean of RequestMappingHandlerAdapter
Just restart tomcat,
Warm reminder: If a method requires authentication, you only need to mark @Auth on the method. If all methods of the class require authentication, you only need to mark @Auth on the class.
Then the question comes. The method interceptor will intercept static resources together. We need to intercept static files in tomcat. For example: My solution is to configure it in web.xml. If you have a good method, you can add it to me. Discuss with 752432995
<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed steps for using zTree’s tree menu
The above is the detailed content of springmvc implements annotation interceptor. For more information, please follow other related articles on the PHP Chinese website!