SpringSecurity5.7以后如何设置静态资源放行
时间: 2024-03-12 10:11:58 浏览: 213
在 Spring Security 5.7 及之后的版本中,你可以使用以下方式设置静态资源的放行:
1. 使用 `permitAll()` 方法:可以使用 `permitAll()` 方法来放行指定的静态资源路径。例如:
```java
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers("/css/**", "/js/**", "/images/**");
}
```
上述代码中,`configure(WebSecurity web)` 方法用于配置 `WebSecurity` 对象,其中 `ignoring()` 方法用于设置不需要进行权限验证的资源,`antMatchers()` 方法用于指定需要放行的资源路径。
2. 使用 `antMatchers().permitAll()`:你也可以直接在 `configure(HttpSecurity http)` 方法中使用 `antMatchers().permitAll()` 来放行静态资源。例如:
```java
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**", "/js/**", "/images/**")
.permitAll()
// 其他配置...
}
```
上述代码中,`configure(HttpSecurity http)` 方法用于配置 `HttpSecurity` 对象,其中 `authorizeRequests()` 方法用于配置请求的权限要求,`antMatchers().permitAll()` 方法用于指定需要放行的资源路径。
以上是两种常用的方式来设置静态资源的放行,你可以根据实际需求选择其中一种方式进行配置。
阅读全文
相关推荐















