SpringSecurity 白名单放行
时间: 2023-10-28 09:01:59 浏览: 395
在 Spring Security 中,你可以通过配置白名单来实现放行某些请求。具体的步骤如下:
1. 创建一个继承自 `WebSecurityConfigurerAdapter` 的配置类。
2. 重写 `configure(HttpSecurity http)` 方法。
3. 在 `configure(HttpSecurity http)` 方法中,使用 `http.authorizeRequests()` 配置请求的权限。
4. 在 `http.authorizeRequests()` 中,使用 `antMatchers()` 方法来匹配需要放行的请求路径。
5. 在 `antMatchers()` 方法中,可以使用通配符 `/**` 匹配所有请求路径,或使用具体的路径进行匹配。
6. 使用 `permitAll()` 方法将匹配的请求路径设置为放行。
下面是一个示例配置类的代码:
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/public/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.logout();
}
}
```
上述代码中,`/public/**` 路径下的请求将被放行,其他请求需要进行认证。
希望对你有所帮助!如有更多问题,请继续提问。
阅读全文
相关推荐












