项目场景:
搭建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)删除