我开始在没有WebSecurityConfigurerAdapter的情况下在我的应用程序中编写web安全性,我在内部使用了使用spring 6的springboot3,在这个设置下,我从下面的代码中得到了一些错误,比如无法解析'ExpressionInterceptUrlRegistry‘中的'antMatchers’方法。
.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();
}发布于 2022-11-29 17:38:05
在spring框架6中,.antMatchers不再可用。相反,根据春季安全文件,您应该执行以下操作:
@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();
}发布于 2022-11-29 08:04:49
由于SpringBoot3.0.0(或SpringFramework6),.antMatchers for AuthorizationManagerRequestMatcherRegistry不再可用。
相反,使用.requestMatchers("/register")
示例:
http.authorizeHttpRequests( auth ->
auth
.requestMatchers(.....)
.anyRequest().authenticated()
)参见关于“授权HttpServletRequests with AuthorizationFilter”的Spring安全文档
https://stackoverflow.com/questions/74588632
复制相似问题