spring boot配置静态资源访问路径

今天遇到个坑就是后台转发前台静态文件时候前台css js找不到文件 全部爆红 后来查找路径也没错 。最终使用spring内部配置解决

@Configuration
@EnableWebMvc
public class MyWebMvcConfigurerConfig extends WebMvcConfigurerAdapter {
    @Resource
    private MyInterceptor myInterceptor;
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        super.addCorsMappings(registry);
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
//                .allowCredentials(true)
//                .allowedHeaders("*")
//                .maxAge(3600);
//
//    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //全部拦截
//        registry.addInterceptor(myInterceptor).addPathPatterns("/**");
        registry.addInterceptor(myInterceptor).excludePathPatterns("/**");

        super.addInterceptors(registry);
    }
    //fixme:配置静态资源访问路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("/","classpath:/");
    }
}

猜你喜欢

转载自www.cnblogs.com/zrboke/p/12912308.html