spring-boot 建立自定義攔截器
配置一
public class TestInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("執行自定義攔截器");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
}
}
配置二
/**
* @description webapp配置
* @author 李銘浩
* @date 2017年8月16日
* @porject spring-boot-helloworld
*/
@Configuration
public class WebAppConfigurer extends WebMvcConfigurerAdapter{
/**
* @description 註冊攔截器
* @author 李銘浩
* @date 2017年8月16日
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}