zoukankan      html  css  js  c++  java
  • spring 拦截器整体配置

    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. }  
  • 相关阅读:
    servlet类中ServletConfig及ServletContext
    jsp及servlet中获取项目路径的一些方法
    EL表达式的一些知识
    python——字符太长,换行需要三个引号。
    python——转义字符
    python——函数缺省参数的使用
    python——字典
    python——if、elif、else的使用
    python——对列表使用的函数方法
    python-创建数字列表
  • 原文地址:https://www.cnblogs.com/panxuejun/p/7476043.html
Copyright © 2011-2022 走看看