忽略springsecurity默认登录验证时报错:java.lang.IllegalStateException: Can‘t configure anyRequest after itself

项目场景:

搭建Springboot+SpringSecurity+JWT的登录项目


问题描述:

搭建好项目后需要启动测试,但引入了springsecurity的项目每次测试的时候需要输入默认账号跟密码!!!!!非常的麻烦,这里就需要忽略每次进入页面时默认的登录验证操作,springboot1.5之前忽略springsecurity通过配置文件的方式,此处贴出springboot2.x的配置方式,通过新建配置类的方式实现忽略验证!!!!

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http);(此处不需要对父类进行引用)
        //配置不需要登录验证
        http.authorizeRequests().anyRequest().permitAll().and().logout().permitAll();
    }
}

如果上处代码对父类进行了引用会抛出异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘springSecurityFilterChain’ defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method ‘springSecurityFilterChain’ threw exception; nested exception is java.lang.IllegalStateException: Can’t configure anyRequest after itself


原因分析:

不应该调用super.configure(http),因为此处使用的是自定义安全配置类的方式实现忽略默认登录验证。
造成此错误的原因是,父配置(http)方法已经调用了.authorizeRequests().anyRequest().authenticated(),并且正如错误消息中提到的那样


解决方案:

解决方案就是将重写后对父类的引用super.configure(http)删除

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值