通过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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...