java - 在anyRequest(多个antMatcher)之后无法配置antMatchers
问题描述
我正在尝试配置 Spring Security 并收到以下错误:
引起:java.lang.IllegalStateException: Can't configure antMatchers after anyRequest
这是我的SecurityConfig
课:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(encodePWD());
}
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.csrf().disable();
http
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/rest/**").permitAll()
.anyRequest().authenticated()
.and()
.authorizeRequests()
.antMatchers("/secure/**").hasAnyRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.permitAll();
http
.authorizeRequests()
.antMatchers("/login").permitAll();
}
@Bean
public BCryptPasswordEncoder encodePWD(){
return new BCryptPasswordEncoder();
}
}
我已经尝试过这里httpSecurityauthorizeRequests().anyRequest().authenticated()
提到的电话,但仍然没有用......任何建议都会有所帮助。
解决方案
修改规则如下。.anyRequest().authenticated()
只能使用一次。
http
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/rest/**").permitAll()
.and()
.authorizeRequests()
.antMatchers("/secure/**").hasAnyRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.permitAll();
推荐阅读
- apache-spark - 在 AWS EMR 上使用 spark;您提供的 AWS 访问密钥 ID 不存在于我们的记录中。但是 boto3 调用工作得很好
- here-api - 为什么从 Free 升级到 u$49.-/month 计划后出现路线错误?
- python - 在张量流中将模型 SSD MobileNetv1 转换为 tflite 时精度低
- amazon-web-services - 创建订阅时未自动修改 AWS SQS 策略
- angular - 没有依赖模板:ConstDependency。使用网络工作者时
- flutter - 颤振给我错误(不知道为什么)(使用颤振和Android Studio)
- vue.js - Vuex 操作未将本地存储令牌添加到 Axios 调用后
- javascript - 带有承诺的 AngularJS 过滤器
- java - JFreeChart | 无法将图形正确定位在 JPanel 内的特定位置
- reactjs - 如何使用钩子防止调度多次执行