Spring Boot使用HandlerInterceptorAdapter和WebMvcConfigurerAdapter实现

org.springframework.web.servlet.handler.HandlerInterceptorAdapter是由spring MVC提供的一个适配器,我们只要继承它,就非常的容易自定义拦截器。

HandlerInterceptorAdaptery有3个方法:

  1. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {
  2. //在业务处理器处理请求之前被调用。预处理,可以进行编码、安全控制等处理;
  3. return true;
  4. }
  5. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)throws Exception {
  6. //在业务处理器处理请求执行完成后,生成视图之前执行。后处理(调用了Service并返回ModelAndView,但未进行页面渲染),有机会修改ModelAndView;
  7. }
  8. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {
  9. //在DispatcherServlet完全处理完请求后被调用,可用于清理资源等。返回处理(已经渲染了页面),可以根据ex是否为null判断是否发生了//异常,进行日志记录;
  10. }

在SpringBoot中使用HandlerInterceptorAdaptery非常的简单,

1、继承HandlerInterceptorAdaptery编写上面3个函数

2、通过Bean注入器把HandlerInterceptorAdaptery注入。

如:
第一步

  1. public class AdminInterceptor extends HandlerInterceptorAdapter {
  2. @Override
  3. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
  4. throws Exception {
  5. HttpSession session = request.getSession();
  6. if (session.getAttribute(WebSecurityConfig.SESSION_KEY) != null)
  7. return true;
  8. // 跳转登录
  9. String url = "/login";
  10. response.sendRedirect(url);
  11. return false;
  12. }
  13. }

第二步 配置全局配置文件,注入bean

  1. package com.jsoft.springboottest.springboottest1.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
  5. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  7. import com.jsoft.springboottest.springboottest1.filter.SecurityInterceptor;
  8. @Configuration
  9. public class WebSecurityConfig extends WebMvcConfigurerAdapter {
  10. /**
  11. * 登录session key
  12. */
  13. public static final String SESSION_KEY = "user";
  14. @Bean
  15. public AdminInterceptor getSecurityInterceptor() {
  16. return new AdminInterceptor();
  17. }
  18. /** 添加拦截器 **/
  19. @Override
  20. public void addInterceptors(InterceptorRegistry registry) {
  21. InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor());
  22. // 排除配置
  23. addInterceptor.excludePathPatterns("/error");
  24. addInterceptor.excludePathPatterns("/login**");
  25. // 拦截配置
  26. addInterceptor.addPathPatterns("/**");
  27. }
  28. }

当然,如果不是用SpringBoot,就不能给我们自动配置
如果基于XML配置使用Spring MVC,可以利用SimpleUrlHandlerMapping、BeanNameUrlHandlerMapping进行Url映射(相当于struts的path映射)和拦截请求(注入interceptors)。

如果基于注解使用Spring MVC,可以使用DefaultAnnotationHandlerMapping注入interceptors。

注意无论基于XML还是基于注解,HandlerMapping Bean都是需要在XML中配置的。