Home > Web Front-end > JS Tutorial > body text

springmvc implements annotation interceptor

php中世界最好的语言
Release: 2018-04-13 17:05:31
Original
1379 people have browsed it

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;
}
Copy after login

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;
		}
	}
}
Copy after login

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>
Copy after login

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>
Copy after login

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:

How to use JSONAPI in PHP

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template