spring-boot 加入拦截器Interceptor

1.spring boot拦截器默认有 

  • HandlerInterceptorAdapter
  • AbstractHandlerMapping
  • UserRoleAuthorizationInterceptor
  • LocaleChangeInterceptor
  • ThemeChangeInterceptor



2.配置spring mvc的拦截器WebMvcConfigurerAdapter 

Java代码   收藏代码
  1. public class WebAppConfig extends WebMvcConfigurerAdapter  



3.实现添加拦截器方法 

Java代码   收藏代码
  1. public void addInterceptors(InterceptorRegistry registry){  
  2.   
  3. }  
  4. registry.addInterceptor可以通过此方法添加拦截器, 可以是spring提供的或者自己添加的  


4.实例部分 

Java代码   收藏代码
    1. public class WebAppConfig extends WebMvcConfigurerAdapter{    
    2.     public static void main(String[] args) {  
    3.         SpringApplication.run(WebAppConfig.class, args);  
    4.     }   
    5.       
    6.     /** 
    7.      * 配置拦截器 
    8.      * @author lance 
    9.      * @param registry 
    10.      */  
    11.     public void addInterceptors(InterceptorRegistry registry) {  
    12.         registry.addInterceptor(new UserSecurityInterceptor()).addPathPatterns("/user/**");  
    13.     }  
    14. }  
    15.   
    16. UserSecurityInterceptor代码  
    17. public class UserSecurityInterceptor implements HandlerInterceptor {  
    18.   
    19.     @Override  
    20.     public boolean preHandle(HttpServletRequest request,  
    21.             HttpServletResponse response, Object handler) throws Exception {  
    22.           
    23.         return true;  
    24.     }  
    25.   
    26.     @Override  
    27.     public void postHandle(HttpServletRequest request,  
    28.             HttpServletResponse response, Object handler,  
    29.             ModelAndView modelAndView) throws Exception {  
    30.     }  
    31.   
    32.     @Override  
    33.     public void afterCompletion(HttpServletRequest request,  
    34.             HttpServletResponse response, Object handler, Exception ex)  
    35.             throws Exception {  
    36.   
    37.     }  
    38.   
    39. }  
5.demo地址 
https://github.com/leelance/spring-boot-all/tree/master/spring-boot-samples
 
想问下,我配置的这个拦截器为啥没有生效呢?
转自:http://lihao312.iteye.com/blog/2078139
 
  1. import org.springframework.context.annotation.Bean;  
  2. import org.springframework.context.annotation.Configuration;  
  3. import org.springframework.web.servlet.config.annotation.InterceptorRegistration;  
  4. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;  
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;  
  6. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;  
  7.   
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10. import javax.servlet.http.HttpSession;  
  11.   
  12. @Configuration  
  13. public class WebSecurityConfig extends WebMvcConfigurerAdapter {  
  14.   
  15.     /** 
  16.      * 登录session key 
  17.      */  
  18.     public final static String SESSION_KEY = "user";  
  19.   
  20.     @Bean  
  21.     public SecurityInterceptor getSecurityInterceptor() {  
  22.         return new SecurityInterceptor();  
  23.     }  
  24.   
  25.     @Override  
  26.     public void addInterceptors(InterceptorRegistry registry) {  
  27.         InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor());  
  28.   
  29.         // 排除配置  
  30. //        addInterceptor.excludePathPatterns("/error");  
  31. //        addInterceptor.excludePathPatterns("/login**");  
  32.   
  33.         // 拦截配置  
  34.         addInterceptor.addPathPatterns("/**");  
  35.     }  
  36.   
  37.     private class SecurityInterceptor extends HandlerInterceptorAdapter {  
  38.   
  39.         @Override  
  40.         public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)  
  41.                 throws Exception {  
  42.             HttpSession session = request.getSession();  
  43.             if (session.getAttribute(SESSION_KEY) != null)  
  44.                 return true;  
  45.   
  46.             // 跳转登录  
  47.             String url = "/login";  
  48.             response.sendRedirect(url);  
  49.             return false;  
  50.         }  
  51.     }  
  52. }  
 

猜你喜欢

转载自www.cnblogs.com/chengjun/p/9234548.html