SpringBoot如何设置不拦截static目录下的静态资源

我们可以在自定义一个组件,继承WebMvcConfigurerAdapter类,然后重写addResourceHandlers()方法,然后使用@Component将这个组件放置到容器当中,

@Component
public class WebMvcConfigureAdapter extends WebMvcConfigurerAdapter {
    
    

    /** 
     * 配置静态访问资源
     * @author xinchao
     * @date 2021/10/17 20:41
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
    
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }
}

本以为用了这个就都解决了问题,但是之后,我发现,没登录,拦截器还是会拦截静态资源。不过可以在配置没登录前拦截器不拦截的请求,

@Configuration
public class LoginConfig implements WebMvcConfigurer {
    
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    
    
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")   // 所有路径都被拦截
                .excludePathPatterns(
                        "/login",
                        "",
                        "/",
                        "/user/login",
                        "/sreimTable",
                        "/smdata",
                        "/static/lib/**");// 静态资源请求不拦截
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42582773/article/details/120816180
今日推荐