我正在将 Spring Security 配置从 WebSecurityConfigurerAdapter(已弃用)迁移到使用 bean 的配置。
我的配置类现在看起来像
@EnableWebSecurity
public class Config {
@Bean
protected SecurityFilterChain configure(HttpSecurity http) throws Exception {
return http.build();
}
}
我知道此配置不会进行任何行为设置,但它不应该返回一个像默认 SecurityFilterChain 一样的 SecurityFilterChain bean (就好像没有指定配置一样)?当我启动我的应用程序时,没有 Controller 受到保护。
最佳答案
要为端点添加安全规则和表单,只需添加 .authorizeRequests()
block :
@Configuration
public class Config {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/unprotected", "/endpoints/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin();
return http.build();
}
}
另请阅读Spring Security without the WebSecurityConfigurerAdapter
关于java - SecurityFilterChain Bean 不保护应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73261956/