端点,Spring Security 配置返回:该方法无法确定模式是否为 Spring MVC

Jam*_*esB 3 spring-security spring-boot spring-framework-beans security-filter

SecurityConfiguration 中的 SecurityFilterChain beans 返回此错误 我没有找到任何有关此方法的信息来解决该问题:

    @Configuration
    @EnableWebSecurity
    public class SecurityConfiguration {
        
          @Bean
            public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
                return  httpSecurity
                        .csrf(csrf -> csrf.disable())
                        .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                        .authorizeHttpRequests(authorize -> authorize
                                .requestMatchers(HttpMethod.POST, "/auth/login").permitAll()
                                .requestMatchers(HttpMethod.POST, "/auth/register").permitAll()
                                .requestMatchers(HttpMethod.POST, "/product").hasRole("ADMIN")
                                .anyRequest().authenticated()
                        )
                        .build();
            }
    }
Run Code Online (Sandbox Code Playgroud)

引起:org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.security.web.SecurityFilterChain]:工厂方法“securityFilterChain”抛出异常并显示消息:此方法无法确定这些模式是否是Spring MVC模式。如果此端点是 Spring MVC 端点,请使用 requestMatchers(MvcRequestMatcher); 否则,请使用 requestMatchers(AntPathRequestMatcher)。

原因:java.lang.IllegalArgumentException:此方法无法确定这些模式是否是 Spring MVC 模式。如果此端点是 Spring MVC 端点,请使用 requestMatchers(MvcRequestMatcher); 否则,请使用 requestMatchers(AntPathRequestMatcher)。

And*_*isa 10

此处描述了原因cve-2023-34035

还有一些关于这个话题的讨论,你可以在这里找到13568

作为解决方法,您可以执行以下操作:

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
    MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector);
    http.authorizeHttpRequests((requests) -> requests
        .requestMatchers(mvcMatcherBuilder.pattern("/test1")).permitAll()
        .anyRequest().authenticated()
    );
    return http.build();
}
Run Code Online (Sandbox Code Playgroud)

更新 至 2023 年 10 月 24 日

关于修复以及将来如何处理这个问题,我建议查看Spring Security 开发团队的下一篇详细信息(issuecomment-1759913041)