SpringBoot filter and filter configuration Servlet3.0

First class start adding @ServletComponentScan, scan

@SpringBootApplication
@ServletComponentScan //过滤器注解 servlet 3.0
public class xxBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(xxBootApplication.class, args);
    }

}

Create a class that implements the Filter and add comments @WebFilter let him mark a class as a filter, is scanned spring 

 

@WebFilter(urlPatterns = "/" ,filterName = "LoginFilter") //urlPatterns  这里匹配拦截的规则 / 表示所有的拦截  可以指定到一个包下面的方法 
public class LoginFilter implements Filter {

    /*
    * 过滤器
    * */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
       if(){
          filterChain.doFilter(servletRequest,servletResponse); //放行
       }else{
          // 不放行
       }

    }
}
自定义拦截器 HandlerInterceptor
Spingboot2.x新版本配置拦截拦截器和旧版本SpringBoot配置拦截器区别讲解
    
    1、@Configuration
        继承WebMvcConfigurationAdapter(SpringBoot2.X之前旧版本)
        SpringBoot2.X 新版本配置拦截器 implements WebMvcConfigurer
    2.自定义拦截器 HandlerInterceptor
        preHandle:调用Controller某个方法之前
        postHandle:Controller之后调用,视图渲染之前,如果控制器Controller出现了异常,则不会执行此方法
        afterCompletion:不管有没有异常,这个afterCompletion都会被调用,用于资源清理
    3.Filter
        是基于函数回调 doFilter(),而Interceptor则是基于AOP思想
        Filter在只在Servlet前后起作用,而Interceptor够深入到方法前后、异常抛出前后等

        依赖于Servlet容器即web应用中,而Interceptor不依赖于Servlet容器所以可以运行在多种环境。
    
        在接口调用的生命周期里,Interceptor可以被多次调用,而Filter只能在容器初始化时调用一次。
        
        Filter和Interceptor的执行顺序
         
        过滤前->拦截前->action执行->拦截后->过滤后
首先新建一个拦截的方法
 
public class LoginIntercepter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.err.println("---->进入方法之前");
        return HandlerInterceptor.super.preHandle(request,response,handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.err.println("---->进入方法之中");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ( "---- After entering method") System.err.println; 
    } 
}

HandlerInterceptor achieve rewriting on the inside and a method in which a request is not to do an operation to define

Then a new class defines interception implementation defined intercepted WebMvcConfigurer

@Configuration 
public class WebMvcConfigurerTest the implements WebMvcConfigurer { 
    // custom in the request interceptor to intercept the files corresponding to the process jumps to LoginIntercepter done on the previous operation request 
    @Override 
    public void addInterceptors (InterceptorRegistry Registry) { 
        Registry. addInterceptor (new new LoginIntercepter ()) addPathPatterns ( "/ **");. 
    } 
}

 拦截器不生效常见问题:
        1)是否有加@Configuration
        2)拦截路径是否有问题 **  和 * 
        3)拦截器最后路径一定要 “/**”, 如果是目录的话则是 /*/

 

Published 10 original articles · won praise 0 · Views 514

Guess you like

Origin blog.csdn.net/DNCCCC/article/details/105031585
Recommended