SpringBoot | 第四十二章:SpringBoot配置静态资源地址和访问路径

前言

      SpringBoot官网静态内容配置介绍如下:SpringBoot静态内容配置官方文档,默认情况下,Spring Boot从类路径中名为/static( /public或/resources或/META-INF/resources)的目录或根目录提供静态内容ServletContext。它使用ResourceHttpRequestHandlerSpring MVC中的from,因此您可以通过添加自己WebMvcConfigurer的addResourceHandlers方法并覆盖该 方法来修改该行为。

在独立的Web应用程序中,还启用了容器中的默认servlet,并将其用作后备,从ServletContextSpring决定不处理它的根开始提供内容。在大多数情况下,这不会发生(除非您修改默认的MVC配置),因为Spring始终可以通过处理请求 DispatcherServlet。

默认情况下,资源映射到/**,但是您可以使用spring.mvc.static-path-pattern属性对其进行调整 。例如,将所有资源重新定位 /resources/**可以通过以下方式实现:

#这表示只有静态资源的访问路径为/resources/**时,才会处理请求
spring.mvc.static-path-pattern=/resources/**,

您还可以通过使用spring.resources.static-locations属性来自定义静态资源位置 (用目录位置列表替换默认值)。根Servlet上下文路径,"/"也会自动添加为一个位置。

1、静态资源路径是指系统可以直接访问的路径,且路径下的所有文件均可被用户通过浏览器直接读取。

 

一、默认的静态资源映射

Spring Boot 对静态资源映射提供了默认配置

在 Spring Boot 中,默认情况下,一共有5个位置可以放静态资源,Spring Boot 默认将 /** 所有访问映射到以下目录,5个路径分别是如下5个:

classpath:/META-INF/resources/

classpath:/resources/

classpath:/static/

classpath:/public/

"/" : 静态资源存放在当前项目的根路径下,放在这个根路径下也是可以访问的

 优先级顺序为:META-INF/resources > resources > static > public > /,下面以a.jpg 、b.jpg 、c.jpg 这三张图片为例子

如:在resources目录下新建 public、resources、static 三个目录,并分别放入 a.jpg 、b.jpg、 c.jpg 图片

SpringBoot | 第四十二章:SpringBoot配置静态资源地址和访问路径_第1张图片

浏览器分别访问:

http://localhost:8080/a.jpg
http://localhost:8080/b.jpg
http://localhost:8080/c.jpg

均能正常访问相应的图片资源。那么说明,Spring Boot 默认会挨个从 public resources static 里面找是否存在相应的资源,如果有则直接返回。

 

二、自定义静态资源映射

方式一:在application.properties配置文件中配置

     如果通过使用配置文件来配置,这里要说明一下下,在application.properties中配置有两项属性,一是“spring.mvc.static-path-pattern”,一是“spring.resources.static-locations”,很多人都难以分辨它们之间的差异,所以经常出现的结果就是404错误,无法找到静态资源。

1. spring.mvc.static-path-pattern

   spring.mvc.static-path-pattern代表的含义是我们应该以什么样的路径来访问静态资源,换句话说,只有静态资源满足什么样的匹配条件,Spring Boot才会处理静态资源请求,以官方默认的配置为例:

#这表示只有静态资源的访问路径为/resources/**时,才会处理请求
spring.mvc.static-path-pattern=/resources/**,

假定采用默认的配置端口,那么只有请求地址类似于“http://localhost:8080/resources/b.jpg”时,Spring Boot才会处理此请求,处理方式是将根据模式匹配后的文件名查找本地文件,那么应该在什么地方查找本地文件呢?这就是“spring.resources.static-locations”的作用了。
 

2. spring.resources.static-locations

spring.resources.static-locations”用于告诉Spring Boot应该在何处查找静态资源文件,这是一个列表性的配置,查找文件时会依赖于配置的先后顺序依次进行,默认的官方配置如下:

#这里表示在何处查找静态资源文件

spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources

继续以上面的请求地址为例,"http://localhost:8080/resources/b.jpg" 就会在上述的四个路径中依次查找是否存在“b.jpg”文件,如果找到了,则返回此文件,否则返回404错误。

 

3. 测试访问静态资源

a. 重启项目,访问:"http://localhost:8080/resources/b.jpg" , 同样能正常访问resources目录下的b.jpg图片资源。

注意:通过spring.mvc.static-path-pattern这种方式配置,会使Spring Boot的默认配置失效,也就是说,/public /resources 等默认配置不能使用。

b. 上述配置中配置了静态模式为/resources/就只能通过/resources/来访问,不过在日常的Web项目开发中,开发者使用SpringBoot框架时,都是把静态资源存放到Static目录下,推荐这种使用方式;

#访问路径为/resources/**时,才会处理请求

spring.mvc.static-path-pattern=/static/** 

 

方式二: 静态资源的Bean配置

在实际开发中,如果需要自定义静态资源访问路径,那么可以继承WebMvcConfigurer类,重写WebMvcConfigurer类来实现。

@Configuration
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private SpringBootPlusProperties springBootPlusProperties;

    @Autowired
    private JwtInterceptor jwtInterceptor;

    @Autowired
    private PermissionInterceptor permissionInterceptor;

    @Autowired
    private TokenTimeoutInterceptor tokenTimeoutInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // JWT拦截器
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(springBootPlusProperties.getInterceptorConfig().getJwtConfig().getExcludePath());

//        // TOKEN超时拦截器
//        registry.addInterceptor(tokenTimeoutInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns(springBootPlusProperties.getInterceptorConfig().getTokenTimeoutConfig().getExcludePath());
//
//        // 权限拦截器
//        registry.addInterceptor(permissionInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns(springBootPlusProperties.getInterceptorConfig().getPermissionConfig().getExcludePath());

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    	registry.addResourceHandler("/templates/**")
    	        .addResourceLocations("classpath:/templates/");
    	registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("classpath:/resources/")
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

上面配置等同于SSM架构中的XML配置,这里就列举配置static和resources为例子。



    

重启项目,访问:http://localhost:8080/resources/b.jpg ,能正常访问resources目录下的b.jpg图片资源。

参考:https://blog.csdn.net/yiifaa/article/details/78299052

你可能感兴趣的:(#,SpringBoot2.x)