分享

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

 明神月 2021-11-05

默认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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多