spring boot配置拦截器和过滤器

1.先定义拦截器的文件内容,如interceptor/BingInterCeptor

public class BingInterCeptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path = request.getServletPath();
        if(path.matches(".*/((.css)|(.js)|(images)|(login)|(anon)).*")){
            return true;
        }else{
            System.out.println("这里开始拦接");
            System.out.println(request.getSession());
            System.out.println(request.getQueryString());
            System.out.println(request.getRequestURI());
            System.out.println(request.getRequestURL());
            System.out.println(request.getPathInfo());

            return false;
        }
       // return false;
    }

2.再在配置文件夹中定义配置类,就可以使用了

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    InterceptorRegistration loginRegistry = registry.addInterceptor(bingInterCeptor()).addPathPatterns("/**"); //拦截所有请求

    // 排除路径
    loginRegistry.excludePathPatterns("/login");
    loginRegistry.excludePathPatterns("/loginout");
    loginRegistry.excludePathPatterns("/view/toLogin");
    loginRegistry.excludePathPatterns("/view/toReg");
    loginRegistry.excludePathPatterns("/user/UserReg");

    loginRegistry.excludePathPatterns("/static/user/css/*.css");
    loginRegistry.excludePathPatterns("/static/user/js/*.js");
    loginRegistry.excludePathPatterns("/static/user/images/*");

    }
    @Bean
    public BingInterCeptor bingInterCeptor(){
        return new BingInterCeptor();
    }

}

原文地址:https://www.cnblogs.com/bing2017/p/15505914.html