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. }  
  • 相关阅读:
    【Repost】Comparision of B-Tree and LSM Tree
    字符串经典算法 Knuth-Morris-Pratt Boyer-Moore AC(Aho-Corasick)算法 后缀自动机
    【Leetcode 913】【Hard】Cat and Mouse 博弈论
    【转】初探计算机视觉的三个源头、兼谈人工智能
    MySQL--06(索引)
    MySQL--05(子查询&视图)
    MySQL--04(聚合函数&表连接查询)
    MySQL--03(增删改查&主键)
    MySQL--02
    MySQL--01
  • 原文地址:https://www.cnblogs.com/panxuejun/p/7476043.html
Copyright © 2011-2022 走看看