如果您在使用 Spring Security 配置中 antMatcher 与 antMatchers 同时使用时遇到问题,可能是由于两者之间的格式不匹配造成的。可以按照以下示例更改配置以解决此问题:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/public/**").permitAll()
.antMatchers("/private/**").authenticated()
.and()
.formLogin();
}
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**");
}
}
在上面的示例中,antMatchers 与 antMatcher 的格式得到了正确的使用。注意到 antMatcher 适用于包装单个请求路径的情况,而 antMatchers 适用于包装多个请求路径的情况。
如果您想拒绝任何未经授权的请求,那么您需要将 antMatchers("/private/").authenticated() 更改为 antMatchers("/private/").hasRole("USER"),以便只允许拥有“USER”角色的用户进入“/private/**”路径下。
注意:以上示例仅供参考,实际上具体如何使用这两者需要根据您的实际使用情况来判断。