Spring boot 春季安全。。5.1.5弹簧释放-安全性。。5.4.6中断安全配置

Spring boot 春季安全。。5.1.5弹簧释放-安全性。。5.4.6中断安全配置,spring-boot,spring-security,spring-security-rest,Spring Boot,Spring Security,Spring Security Rest,摘要 将Spring boot项目从2.1.5版本更新为2.4.5版本。它自动更新了版本Spring Security中的所有Spring安全依赖项。。5.1.5弹簧释放-安全性。。5.4.6中断安全配置 原因:org.springframework.beans.beans实例化异常:未能实例化[javax.servlet.Filter]:工厂方法“springSecurityFilterChain”引发异常;嵌套异常为java.lang.IllegalStateException:无法在其自身

摘要 将Spring boot项目从2.1.5版本更新为2.4.5版本。它自动更新了版本Spring Security中的所有Spring安全依赖项。。5.1.5弹簧释放-安全性。。5.4.6中断安全配置

原因:org.springframework.beans.beans实例化异常:未能实例化[javax.servlet.Filter]:工厂方法“springSecurityFilterChain”引发异常;嵌套异常为java.lang.IllegalStateException:无法在其自身之后配置任何请求 在org.springframework.beans.factory.support.simpleinstationstrategy.instantiate(simpleinstationstrategy.java:185)~[spring-beans-5.3.6.jar:5.3.6] 在org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)~[spring-beans-5.3.6.jar:5.3.6] ... 28多 原因:java.lang.IllegalStateException:无法在自身之后配置任何请求 在org.springframework.util.Assert.state(Assert.java:76)~[spring-core-5.3.6.jar:5.3.6] 在org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry.anyRequest(AbstractRequestMatcherRegistry.java:72)~[spring-security-config-5.4.6.jar:5.4.6] 在com.verizon.wfm.nt.config.SecurityConfig.configure(SecurityConfig.java:14)~[default/:?] 在org.springframework.security.config.annotation.web.configuration.websecurityConfigureAdapter.getHttp(websecurityConfigureAdapter.java:217)~[spring-security-config-5.4.6.jar:5.4.6]

安全配置 工作代码

@EnableWebSecurity
@configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@override
protected void configure(HttpSecurity httpSecurity) throws Exception {
super.configure(httpSecurity);
httpSecurity.authorizeRequests().anyRequest().permitAll();
httpSecurity.csrf().disable();
httpSecurity.headers().frameOptions().disable();
}
}

调用
super.configure(httpSecurity)
时,它会执行以下操作:

http.authorizeRequests((requests)->requests.anyRequest().authorized());
http.formLogin();
http.httpBasic();
之后,您将再次使用
anyRequest
配置请求。在最新版本的Spring Security中不允许使用它

我建议您不要调用
super.configure(httpSecurity)
,而是禁用默认值并进行配置,如下所示:

@EnableWebSecurity
@配置
公共类SecurityConfig扩展了WebSecurity配置适配器{
@凌驾
受保护的无效配置(HttpSecurity HttpSecurity)引发异常{
httpSecurity.formLogin().disable();
httpSecurity.httpBasic().disable();
httpSecurity.authorizeRequests((请求)->
requests.anyRequest().permitAll()
);
httpSecurity.csrf().disable();
httpSecurity.headers().frameOptions().disable();
}
}

它的工作原理如下!!谢谢你,马库斯。httpSecurity.formLogin().disable();httpSecurity.httpBasic().disable();//httpSecurity.authorizeRequests((请求)->//requests.anyRequest().permitAll());httpSecurity.authorizeRequests().anyRequest().permitAll();httpSecurity.csrf().disable();httpSecurity.headers().frameOptions().disable();很好,你介意接受答案吗?是的,马库斯。谢谢!