springboot中静态资源存放位置
时间: 2023-10-18 16:21:32 浏览: 54
在Spring Boot中,静态资源默认存放在classpath下的/static、/public、/resources和/META-INF/resources目录下。可以通过application.properties或application.yml文件来修改默认存放位置,如:
```
# application.properties
spring.resources.static-locations=classpath:/static/,classpath:/public/,classpath:/myresources/
```
```
# application.yml
spring:
resources:
static-locations: classpath:/static/,classpath:/public/,classpath:/myresources/
```
这里的示例将静态资源存放在classpath下的/static、/public和/myresources目录下。
相关问题
springboot 静态资源黑名单
在Spring Boot中,可以通过配置静态资源黑名单来限制某些资源的访问。静态资源包括css、js、图片等文件,它们存放在项目的资源文件夹中,可以直接通过URL访问。
要配置静态资源的黑名单,可以使用Spring Security框架提供的功能。首先,需要在项目的配置文件(application.properties或application.yml)中配置Spring Security的相关信息,包括启用安全认证和配置访问规则。
例如,在application.yml中添加以下配置:
```
spring:
security:
enabled: true
resources:
static-locations: classpath:/static/
```
上述配置指定了静态资源的位置为classpath:/static/,即项目的resources/static目录。接下来,可以创建一个安全配置类来设置访问规则。
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/blacklist/**").denyAll() // 配置黑名单资源的访问规则
.anyRequest().permitAll(); // 其他资源允许访问
}
}
```
上述配置指定了"/blacklist/**"路径下的资源不允许访问,其他路径下的资源允许访问。可以根据实际需求修改配置,例如设置更复杂的路径匹配规则、自定义访问权限等。
通过上述配置,静态资源的黑名单就设置好了。当用户访问被限制的资源时,将会收到403 Forbidden的响应。可以根据需要增加登录认证、角色授权等功能来提升安全性。
springboot 资源下载
在Spring Boot中,可以通过配置文件来设置资源文件的存放位置。一般而言,资源文件可以存放在项目的classpath下的各个文件夹中。比如,静态资源文件(如html、js、css等)可以存放在src/main/resources/static目录下,模板文件(如Thymeleaf、Freemarker等)可以存放在src/main/resources/templates目录下。通过这种方式,Spring Boot会自动识别并加载这些资源文件。
另外,Spring Boot还支持使用webjars来管理和引入前端资源,如jQuery、Bootstrap等。通过在pom.xml文件中添加相关依赖,Spring Boot会自动将对`/webjars/**`的访问重定向到`classpath:/META-INF/resources/webjars/**`下的资源文件。这样,我们可以直接在前端页面中引用这些资源文件。
总结起来,Spring Boot提供了多种方式来管理和使用资源文件,可以根据具体的需求选择合适的方式进行资源下载。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [Springboot静态资源访问、上传、回显和下载](https://blog.csdn.net/qq_34491508/article/details/102955564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]