Spring Security antmacher配置在Spring6中不起作用

bqujaahr  于 2022-11-29  发布在  Spring
关注(0)|答案(1)|浏览(1096)

我在没有WebSecurityConfigurerAdapter的情况下开始在我的应用程序中编写Web安全性,我在内部使用了springboot3,它在此设置下使用了spring 6。我在以下代码中遇到了类似于Cannot resolve method 'antMatchers' in 'ExpressionInterceptUrlRegistry'的错误

.authorizeRequests()
                    .antMatchers("/register").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .httpBasic()

完整方法

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

                .authorizeRequests()
                .antMatchers("/register").permitAll()
                .anyRequest().authenticated()
                .and()
                .httpBasic()

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

        return http.build();
    }
vnzz0bqm

vnzz0bqm1#

从Sping Boot 3.0.0(或Spring Framework 6)开始,.antMatchers for AuthorizationManagerRequestMatcherRegistry不再可用。
请改为使用.requestMatchers("/register")(若要进行更多自定义,请使用.requestMatchers(new AntPathRequestMatcher()
示例:

http.authorizeHttpRequests( auth ->
    auth
        .requestMatchers(.....)
        .anyRequest().authenticated()
    )

请参阅有关 “使用授权过滤器授权HttpServletRequests” 的Spring安全文档:
https://docs.spring.io/spring-security
请参阅AntPathRequestMatcher的链接:
AntPathRequestMatcher

相关问题