springboot 2.0+auth2 resources: static-locations 外部资源拦截

默认Springboot将从如下位置按如下顺序加载jar包对应前端静态资源:

1.jar包同级static目录
2.jar包同级public目录
3.jar包同级resource目录
4.jar包/META-INF/resources

在调试模式下,Springboot将从class目录中按如下顺序加载对应前端静态资源

1.class目录下static目录
2.class目录下public目录
3.class目录下resource目录
4.class目录下/META-INF/resources

通过设置spring.resources.static-locations自定义Spring boot加载前端静态资源路径

spring.resources.static-locations: file:D:/public/
亦可以指定先后顺序:
spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resource

如果指定了拦截器,该属性有可能失效,需要在拦截器处排除映射前缀

:
  : /**
:
  : classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:d:/spas/data
关键java代码
public class AuthWebMvcConfigurer implements WebMvcConfigurer {
@Value("${mvp51.file-upload.local.path}")
private String path;

@Override
public void addInterceptors(InterceptorRegistry registry) {

    registry
            .addInterceptor(new AuthInterceptor())
            .excludePathPatterns("/static/**")
            .excludePathPatterns("/upload/**")
            .order(-100);
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/upload/**")
         .addResourceLocations("classpath:/static/")
. dResourceLocations("file:"+path+"/");
}
http://localhost:9080/api/upload/test2/apple.jpg
(0)

相关推荐