spring boot 2.x interceptor

1. Spring1.x configuration method

    In spring boot1.x, the interceptor is used, and the following configuration is generally performed:

@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {
	@Resource
	private FRInterceptor fRInterceptor;

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//自定义拦截器,添加拦截路径和排除拦截路径 
		registry.addInterceptor(fRInterceptor).addPathPatterns("api/**").excludePathPatterns("api/login"); ;
	}
}

    

    But in spring boot2.x, WebMvcConfigurerAdapter is deprecated. Although inheriting the WebMvcConfigurerAdapter class is convenient, it has been deprecated in Spring 5.0.

     The official documentation also says that the WebMvcConfigurer interface now has a default blank method, so it is better to implement WebMvcConfigurer under Springboot2.0 (Spring5.0) .

2, spring2.x configuration method

2.1 Unified management of interceptors

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.spring.pro.interceptor.FileUploadInterceptor;

/**
 * @ClassName: WebConfig
 * @Description:
 * @author weiyb
 * @date 2018年5月7日 上午11:30:58
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
	@Resource
	private FileUploadInterceptor fileUploadInterceptor;

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 自定义拦截器,添加拦截路径和排除拦截路径
		registry.addInterceptor(fileUploadInterceptor).addPathPatterns("/**");
	}
}

2.2 Custom Interceptors

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 文件上传拦截器
 * @ClassName: FileUploadInterceptor
 * @Description:
 * @author weiyb
 * @date 2018年5月7日 上午11:51:53
 */
@Component
public class FileUploadInterceptor implements HandlerInterceptor {
	/*
	 * 视图渲染之后的操作
	 */
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {

	}

	/*
	 * 处理请求完成后视图渲染之前的处理操作
	 */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {

	}

	/*
	 * 进入controller层之前拦截请求
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		System.out.println("getContextPath:" + request.getContextPath());
		System.out.println("getServletPath:" + request.getServletPath());
		System.out.println("getRequestURI:" + request.getRequestURI());
		System.out.println("getRequestURL:" + request.getRequestURL());
		System.out.println("getRealPath:" + request.getSession().getServletContext().getRealPath("image"));
		return true;
	}

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326218002&siteId=291194637