如何在AbstractHttpConfigurer的 *before* 本地HttpSecurity代码中实现config

avwztpqn  于 12个月前  发布在  Spring
关注(0)|答案(1)|浏览(144)

我正在将内部库和应用程序更新到Sping Boot 3(已更新到Spring Security 6)。
我们有共同的安全配置,我已经更新,以实现AbstractHttpConfigurer,例如。

public class WebberWebSecurityConfigurerAdapter
    extends AbstractHttpConfigurer<WebberWebSecurityConfigurerAdapter, HttpSecurity> {

  public void configure(final HttpSecurity http) throws Exception {
     http
      .authorizeHttpRequests()
      .requestMatchers(HEALTH_CHECK_PATH).permitAll()
  }
}

字符串
然后,Web应用程序具有:

@Configuration
public class SecurityConfig  {

@Bean
public SecurityFilterChain config(HttpSecurity http) throws Exception {
  http.apply(new WebberWebSecurityConfigurerAdapter());
  http
      .authorizeHttpRequests()
      .requestMatchers("/", "/request-info", "/test").permitAll()
      .anyRequest().authenticated();
  return http.build();
}


}
这会抛出异常:

Caused by: java.lang.IllegalStateException: Can't configure requestMatchers after anyRequest


大概是因为Spring试图在.anyRequest().authenticated();行 * 之后配置.requestMatchers(HEALTH_CHECK_PATH).permitAll()行,尽管它在config中被首先指定。
如何让Spring在应用Web应用程序本身的自定义配置之前,先配置所有上游库配置?

6tqwzwtp

6tqwzwtp1#

尝试使用antMatcher()方法而不是anyRequest()。

@Configuration
public class SecurityConfig  {

@Bean
public SecurityFilterChain config(HttpSecurity http) throws Exception {
  http.apply(new WebberWebSecurityConfigurerAdapter());
  http
      .authorizeHttpRequests()
      .requestMatchers("/", "/request-info", "/test").permitAll()
      .requestMatchers("/**").authenticated();
  return http.build();
}

字符串

相关问题