当前位置:   article > 正文

Spring boot拦截器+不拦截静态资源_springboot过滤器默认不过滤static下的文件吗

springboot过滤器默认不过滤static下的文件吗

继承handlerInterceptor的拦截器

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null || user.getUsername() == null || user.getUsername().equals("")) {
            response.sendRedirect("/login");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

配置

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    private final String dirName = System.getProperty("user.dir") + "\\images\\car\\";

    private final LoginInterceptor loginInterceptor;
    private final Logger logger = LoggerFactory.getLogger(FileService.class);
    public WebMvcConfig(LoginInterceptor loginInterceptor) {
        this.loginInterceptor = loginInterceptor;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String dir = dirName.replace("\\", "/");
        logger.info(dir);
        registry.addResourceHandler("/img/car/**")
                .addResourceLocations("file:///" + dir);
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .excludePathPatterns("/login")  //不拦截的
                .excludePathPatterns("/dist/**")
                .excludePathPatterns("classpath:/static/**")
                .excludePathPatterns("/carinfo/up").excludePathPatterns("/carinfo/upb")
                .addPathPatterns("/**");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/972971
推荐阅读
相关标签
  

闽ICP备14008679号