springboot静态资源放行
时间: 2023-11-14 21:12:17 浏览: 44
在Spring Boot中,可以通过以下几种方式来放行静态资源:
1. 默认情况下,Spring Boot会自动将静态资源文件夹(如src/main/resources/static)下的文件放行,可以直接通过URL访问。
2. 如果需要指定静态资源前缀,可以通过在application.properties或application.yml中配置spring.mvc.static-path-pattern属性来实现。
3. 如果需要支持静态资源欢迎页(如index.html),可以将该文件放置在静态资源文件夹下,并且不配置静态资源前缀。
需要注意的是,如果使用了自定义的静态资源前缀,需要在URL中加上该前缀才能访问静态资源。
相关问题
springboot 静态资源放行
SpringBoot中的静态资源指的是一些html、js、css、图片等文件,这些文件可以在项目中通过URL路径直接访问。默认情况下,SpringBoot会把静态资源放在`/static`、`/public`、`/resources`和`/META-INF/resources`这几个目录下面,而且这些目录下的所有文件都可以被直接访问。
如果你需要在这些目录之外再添加一些静态资源,可以通过在`application.properties`文件中添加以下配置来实现:
```
spring.resources.static-locations=file:/path/to/your/static/resources/
```
其中,`file:/path/to/your/static/resources/`表示你需要添加的静态资源所在的目录。需要注意的是,在Linux系统下需要加上`file:`前缀才能识别。
此外,还可以通过添加`WebMvcConfigurer`的实现类来实现对静态资源的放行。具体代码如下:
```
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}
```
以上代码表示,将`/static/**`路径下的请求映射到`classpath:/static/`路径下的文件。
springboot前端的静态资源怎么放行
Spring Boot前端的静态资源可以通过在application.properties或application.yml文件中添加以下配置来放行:
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/
其中,/static/**是指静态资源的URL路径,classpath:/static/是指静态资源的存储路径。这样配置后,所有/static/**的请求都会被转发到classpath:/static/目录下寻找对应的资源。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)