You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring 6中antMatchers方法无法解析问题求助

解决Spring Boot 3中antMatchers方法无法解析的问题

在Spring Boot 3(基于Spring 6)对应的Spring Security 6版本中,authorizeRequests()和antMatchers()方法已被废弃,这就是你遇到Cannot resolve method 'antMatchers' in 'ExpressionInterceptUrlRegistry'错误的原因。

修改后的完整配置代码

@Bean
protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
            .cors()
            .and()
            .csrf()
            .disable()

            .authorizeHttpRequests(auth -> auth
                    .requestMatchers("/register").permitAll()
                    .anyRequest().authenticated())
            .httpBasic()

            .and()
            .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
            .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

    return http.build();
}

关键修改点

  • 替换.authorizeRequests()为.authorizeHttpRequests(),推荐使用lambda表达式的写法来简化配置逻辑
  • 替换.antMatchers("/register")为.requestMatchers("/register"),requestMatchers()是Spring Security 6中用于匹配请求路径的新方法,功能与旧方法一致,API更统一规范

内容的提问来源于stack exchange,提问作者Pradeep Virtuele

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 00:20:27