spring boot使用HandlerInterceptorAdapter+WebMvcConfigurerAdapter拦截
最近在做考试系统,是个微信小程序,页面都是前端那里做好的,数据都是调的后端接口,自然后端就使用到了拦截器。时间有点紧,这篇只是大概写个结构,以后再补。
1.HandlerInterceptorAdapter
preHandle 业务处理器在处理业务之前被调用,预处理。
postHandle 在业务处理之后调用,后处理,
afterCompletion 在DispatcherServlet完全处理完请求后被调用,可用于清理资源。也可以根据ex是否为null来判断是否发生了异常。
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author suwan */ public class LoginInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String url = request.getRequestURI().toString(); System.out.println("preHandle:" + url); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { String url = request.getRequestURI().toString(); System.out.println("postHandle:" + url); super.postHandle(request, response, handler, modelAndView); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion:" + ex); super.afterCompletion(request, response, handler, ex); } }
2.WebMvcConfigurerAdapter添加拦截器
addInterceptors 需要一个实现HandlerInterceptor接口的拦截器实例
addPathPatterns 用于设置拦截器的过滤路径规则
excludePathPatterns:用于设置不需要拦截的过滤规则
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * @author suwan */ @Configuration public class InterceptorConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(loginInterceptor()).addPathPatterns("/login/**").excludePathPatterns("/hello/**"); } @Bean public LoginInterceptor loginInterceptor(){return new LoginInterceptor();} }
3.测试设置拦截器的路径
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author suwan */ @RestController @RequestMapping("/login") public class UserController { @GetMapping("/test") public void test(){ System.out.println("loginTest"); } }
浏览器请求:http://localhost:8080/login/test
输出: preHandle:/login/test loginTest postHandle:/login/test afterCompletion:null
4.测试未设置拦截的路径
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author suwan * @date 2019/6/13 */ @RestController @RequestMapping("/hello") public class HelloController { @GetMapping("/test") public void test(){ System.out.println("helloTest"); } }
浏览器请求:http://localhost:8080/hello/test
输出:
helloTest