通过Google和Yandex进行OAuth身份验证

问题描述

通过Google和Yandex创建OAuth身份验证时出现下一个错误。怎么解决呢?还是有另一种方法

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

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)