'authorizeRequests()' 已被弃用 导致 .antMatchers(AUTH_WHITELIST).permitAll()功能无法正常使用,
有没有什么解决方案
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
@Bean
SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
//省略HttpSecurity的配置
/**
* 配置请求拦截
*/
httpSecurity.cors().and()
//由于使用的是JWT,我们这里不需要csrf
.csrf().disable()
//基于token,所以不需要session
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
//可以匿名访问的链接
.antMatchers(AUTH_WHITELIST).permitAll()
//其他所有请求需要身份认证
.anyRequest().authenticated()
.and()
//.addFilter(new JWTLoginFilter(authenticationManager()))
.addFilter(new JWTAuthenticationFilter(authenticationManager()));
return httpSecurity.build();
}
可以使用http.authorizeRequests()代替。对于.antMatchers(AUTH_WHITELIST).permitAll()无法正常使用的问题,可以尝试使用http.authorizeRequests().antMatchers(AUTH_WHITELIST).permitAll()来实现同样的功能。同时,你也可以使用其他的权限控制方式来代替authorizeRequests(),比如@PreAuthorize注解或者自定义的AccessDecisionManager实现。
你的security版本是多少
这个可以吗
.authorizeRequests().mvcMatchers(AUTH_WHITELIST).permitAll()