2

通过 Google 和 Yandex 创建 OAuth 身份验证时出现下一个错误。如何解决?还是有其他方法?

org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration 中的方法 userInfoRestTemplateFactory 需要一个 bean,但是找到了 2 个:

  • yandex:由类路径资源 [ppers/conrem/config/WebSecurityConfig.class] 中的方法 'yandex' 定义

  • google:由类路径资源 [ppers/conrem/config/WebSecurityConfig.class] 中的方法“google”定义

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;
    }
}
4

0 回答 0