2

I have the next error when create OAuth Authentication through Google and Yandex. How to resolve it? Or is there another way?

Method userInfoRestTemplateFactory in org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration required a single bean, but 2 were found:

  • yandex: defined by method 'yandex' in class path resource [ppers/conrem/config/WebSecurityConfig.class]

  • google: defined by method 'google' in class path resource [ppers/conrem/config/WebSecurityConfig.class]

WebSecurityConfig.class

@Configuration
@EnableWebSecurity
@EnableOAuth2Sso
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Qualifier("oauth2ClientContext")
    @Autowired
    private OAuth2ClientContext oauth2ClientContext;

    @Autowired
    private UserDetailsRepo userDetailsRepo;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .mvcMatchers("/").permitAll()
                .anyRequest().authenticated()
                .and()
                    .csrf().disable();

        http
                .addFilterBefore(ssoFilter(), UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    @ConfigurationProperties("yandex.client")
    public AuthorizationCodeResourceDetails yandex() {
        return new AuthorizationCodeResourceDetails();
    }

    @Bean
    @ConfigurationProperties("yandex.resource")
    public ResourceServerProperties yandexResource() {
        return new ResourceServerProperties();
    }

    @Bean
    @ConfigurationProperties("google.client")
    public AuthorizationCodeResourceDetails google() {
        return new AuthorizationCodeResourceDetails();
    }

    @Bean
    @ConfigurationProperties("google.resource")
    public ResourceServerProperties googleResource() {
        return new ResourceServerProperties();
    }

    @Bean
    public FilterRegistrationBean<OAuth2ClientContextFilter> oauth2ClientFilterRegistration(
            OAuth2ClientContextFilter filter) {
        FilterRegistrationBean<OAuth2ClientContextFilter> registration =
                new FilterRegistrationBean<OAuth2ClientContextFilter>();
        registration.setFilter(filter);
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
        return registration;
    }

    private Filter ssoFilter() {
        CompositeFilter filter = new CompositeFilter();
        List<Filter> filters = new ArrayList<>();
        SimpleUrlAuthenticationSuccessHandler successHandler = new SimpleUrlAuthenticationSuccessHandler();
        successHandler.setAlwaysUseDefaultTargetUrl(true);
        successHandler.setDefaultTargetUrl("/user");

        // Yandex
        OAuth2ClientAuthenticationProcessingFilter yandexFilter =
                new OAuth2ClientAuthenticationProcessingFilter("/login/yandex");
        OAuth2RestTemplate yandexTemplate = new OAuth2RestTemplate(yandex(), oauth2ClientContext);
        yandexFilter.setRestTemplate(yandexTemplate);
        UserInfoTokenServices yandexTokenServices = new UserInfoTokenServices(
                yandexResource().getUserInfoUri(),
                yandex().getClientId()
        );
        yandexTokenServices.setRestTemplate(yandexTemplate);
        yandexTokenServices.setPrincipalExtractor(new YandexPrincipalExtractor(userDetailsRepo));

        yandexFilter.setTokenServices(yandexTokenServices);
        yandexFilter.setAuthenticationSuccessHandler(successHandler);

        filters.add(yandexFilter);

        // Google
        OAuth2ClientAuthenticationProcessingFilter googleFilter =
                new OAuth2ClientAuthenticationProcessingFilter("/login/google");
        OAuth2RestTemplate googleTemplate = new OAuth2RestTemplate(google(), oauth2ClientContext);
        googleFilter.setRestTemplate(googleTemplate);
        UserInfoTokenServices googleTokenServices = new UserInfoTokenServices(
                googleResource().getUserInfoUri(),
                google().getClientId()
        );
        googleTokenServices.setRestTemplate(googleTemplate);
        googleTokenServices.setPrincipalExtractor(new GooglePrincipalExtractor(userDetailsRepo));

        googleFilter.setTokenServices(googleTokenServices);
        googleFilter.setAuthenticationSuccessHandler(successHandler);

        filters.add(googleFilter);

        filter.setFilters(filters);
        return filter;
    }
}
Ivan
  • 21
  • 1

0 Answers0