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
相关产品推荐
相关产品推荐

