为了账号安全,请及时绑定邮箱和手机立即绑定

构建“springSecurityFilterChain”时出现

构建“springSecurityFilterChain”时出现

湖上湖 2022-12-15 17:02:55
我正在使用 Spring Security 开发 Spring Boot 应用程序,但无法以任何方式运行。我想使用 Spring Security 5,但此时我什至不知道我要使用哪个版本,使用 WebFlux 也无济于事。问题是,当我创建一个扩展 WebSecurityConfigurerAdapter 的类时,Spring 总是会抛出org.springframework.security.config.annotation.AlreadyBuiltException: This object has already been builtafter 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 org.springframework.security.config.annotation.AlreadyBuiltException: This object has already been built,我不知道为什么。我尝试删除@Configuration注释,@EnableWebSecurity一个,两个,并添加其他注释,例如@Order,但添加或删除注释似乎没有帮助。
查看完整描述

1 回答

?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

Caused by: org.springframework.security.config.annotation.AlreadyBuiltException: This object has already been built

    at org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:44) ~[spring-security-config-5.1.4.RELEASE.jar:5.1.4.RELEASE]

    at org.springframework.security.config.annotation.web.builders.WebSecurity.performBuild(WebSecurity.java:294) ~[spring-security-config-5.1.4.RELEASE.jar:5.1.4.RELEASE]

    at org.springframework.security.config.annotation.web.builders.WebSecurity.performBuild(WebSecurity.java:79) ~[spring-security-config-5.1.4.RELEASE.jar:5.1.4.RELEASE]

    at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:334) ~[spring-security-config-5.1.4.RELEASE.jar:5.1.4.RELEASE]

当HttpSecurity#build()每个实例被调用多次时,就会发生这种情况。HttpSecurity#build()配置时不需要调用,HttpSecurity因为 spring security 会在后台调用它。所以删除应该可以build()解决configure(HttpSecurity http)问题:


@Override

public void configure(HttpSecurity http) throws Exception {

    http.csrf().disable().authorizeRequests()

            .antMatchers("/*/inserir").hasAuthority(ROLE_ULTRA)

            .anyRequest().authenticated()

            .and().formLogin();

}


查看完整回答
反对 回复 2022-12-15
  • 1 回答
  • 0 关注
  • 546 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号