类路径中已经定义了相同名称的bean,因此无法注册Bean 'scopedTarget.oauth2ClientContext'。

6

我只是想在我的应用程序中启用谷歌登录功能,但遇到了这个特定的错误。我不理解这个问题为什么会出现?我需要帮助解决这个错误吗?有什么提示吗?

@Configuration
@EnableOAuth2Sso
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf()
                    .disable()
                .antMatcher("/**")
                .authorizeRequests()
                .antMatchers("/", "/welcomepage")
                    .permitAll()
                .anyRequest()
                .authenticated();
    }
}

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'scopedTarget.oauth2ClientContext', defined in class path resource [org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2RestOperationsConfiguration$SessionScopedConfiguration$ClientContextConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/security/oauth2/config/annotation/web/configuration/OAuth2ClientConfiguration$OAuth2ClientContextConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
3个回答

21

我曾经遇到过同样的问题。我通过升级spring-security-oauth2-autoconfigure库的版本到2.1.1.RELEASE解决了它。

    <dependency>
        <groupId>org.springframework.security.oauth.boot</groupId>
        <artifactId>spring-security-oauth2-autoconfigure</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>

我在2.1.3.RELEASE上仍然遇到这个问题,现在它只是说重复的两个都在OAuth2RestOperationsConfiguration.class中,而不是一个在那里,另一个在其他地方。看起来它只是把问题转移了。 - user10677470

4

在SpringBoot 2.0之前,SpringBoot通常会覆盖旧的bean。但是这种情况已经改变了。对于我来说,在我的application.yml文件中添加spring.main.allow-bean-definition-overriding: true作为第一行就能解决问题。


4

我遇到了相同的问题,并已经解决。

只需要将spring-security-oauth2-autoconfigure版本从2.0.0.RELEASE改为2.1.1.RELEASE即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接