我有这个Spring Security代码,我想为Spring Security 6配置:
public void configure(final HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(permittedAntMatchers())
.permitAll();
http.authorizeRequests().antMatchers("/**").authenticated();
.....
http.exceptionHandling()....;
}
我得到Cannot resolve method 'antMatchers' in 'ExpressionInterceptUrlRegistry'
的错误。你知道我怎样才能正确地迁移这个项目吗?
在升级到Spring 6之前,您可以升级到Spring安全5.8。X版本准备春季安全6。这里有迁移文档,可以帮助您了解需要更改的内容。
Spring Security 6删除了WebSecurityConfigurerAdapter
类。所以不能用public void configure(HttpSecurity http) throws Exception {
法。相反,您需要创建SecurityFilterChain Bean
。
您的代码如下所示:
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests().requestMatchers(permittedAntMatchers()).permitAll();
http.authorizeHttpRequests().requestMatchers(AntPathRequestMatcher.antMatcher("/**")).authenticated();
http.exceptionHandling().....;
return http.build();
}
RequestMatcher[] permittedAntMatchers() {
List<RequestMatcher> matchers = new ArrayList<>();
matchers.add(AntPathRequestMatcher.antMatcher(HttpMethod.POST, "/users/*")); // Sample
// Add all your matchers
return matchers.toArray(new RequestMatcher[0]);
}