EnableOAuth2Sso for multiple social networks simultaneously

I am implementing a spring boot application that should provide OAuth2 authorization authorization and support for several social services (google +, facebook, etc.). The user should be able to select their preferred social network and log in using the OAuth2 authorization system. I implement this using the http://cloud.spring.io/spring-cloud-security/ approach described here .

Currently my application.yml is as follows

spring:
  oauth2:
    client:
      clientId: {{my app google id}
      clientSecret: {{my app google secret code}} 
  etc...

In addition, the main spring boot class is annotated as @ EnableOAuth2Sso.

My problem is that with the above approach, I can only use one social network for my own purposes (google + in the example). So, I would like to know if there is a way to support several social networks at the same time, defining them somehow in the .yml file. Is this possible or should I use a different approach?

thank you for your time,

Chris

+3
source share
4 answers

@EnableOAuth2Sso YAML. 2 OAuth2AuthenticationProcessingFilters , , .

+4

, , , ( , ​​..): social_login_manual

, @EnableOAuth2Sso @EnableOAuth2Client . , . , @EnableOAuth2Client , @EnableOAuth2Sso .

, :

@SpringBootApplication
@EnableOAuth2Client
@RestController
public class SocialApplication extends WebSecurityConfigurerAdapter {

   private Filter ssoFilter() {
      CompositeFilter filter = new CompositeFilter();
      List<Filter> filters = new ArrayList<>();
      filters.add(ssoFilter(facebook(), "/login/facebook"));
      filters.add(ssoFilter(github(), "/login/github"));
      filter.setFilters(filters);
      return filter;
    }



 private Filter ssoFilter(ClientResources client, String path) {
      OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(path);
      OAuth2RestTemplate template = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
      filter.setRestTemplate(template);
      filter.setTokenServices(new UserInfoTokenServices(
          client.getResource().getUserInfoUri(), client.getClient().getClientId()));
      return filter;
    }

  @Bean
    @ConfigurationProperties("github")
    public ClientResources github() {
      return new ClientResources();
    }

  @Bean
    @ConfigurationProperties("facebook")
    public ClientResources facebook() {
      return new ClientResources();
    }

}

class ClientResources {

  @NestedConfigurationProperty
  private AuthorizationCodeResourceDetails client = new AuthorizationCodeResourceDetails();

  @NestedConfigurationProperty
  private ResourceServerProperties resource = new ResourceServerProperties();

  public AuthorizationCodeResourceDetails getClient() {
    return client;
  }

  public ResourceServerProperties getResource() {
    return resource;
  }
}

facebook:
  client:
    clientId: 233668646673605
    clientSecret: 33b17e044ee6a4fa383f46ec6e28ea1d
    accessTokenUri: https://graph.facebook.com/oauth/access_token
    userAuthorizationUri: https://www.facebook.com/dialog/oauth
    tokenName: oauth_token
    authenticationScheme: query
    clientAuthenticationScheme: form
  resource:
    userInfoUri: https://graph.facebook.com/me
github:
  client:
    clientId: bd1c0a783ccdd1c9b9e4
    clientSecret: 1a9030fbca47a5b2c28e92f19050bb77824b5ad1
    accessTokenUri: https://github.com/login/oauth/access_token
    userAuthorizationUri: https://github.com/login/oauth/authorize
    clientAuthenticationScheme: form
  resource:
    userInfoUri: https://api.github.com/user
+1

. , @EnableOAuth2Sso , . .

Nimbus OAuth . , , , , . URI.

: https://bitbucket.org/klaalo/orcidconnect/src/f3e4fada9827e47bd33efd579fd020c41e37ee2a/src/main/java/fi/csc/orcidconnect/oauth2client/

, , DelegatingAuthenticationProviderEndpoint SecurityConfiguration ( ). AuthenticationProcessingFilter . AuthenticationToken AuthenticationProvider .

UserDetails User .

, .

0
source

The link you provided shows how to use the @ EnableOAuth2Sso tools that allow a single authentication server provider. To implement multiple providers, you must follow:

https://spring.io/guides/tutorials/spring-boot-oauth2/#_social_login_manual

and implement a filter for each provider.

0
source

Source: https://habr.com/ru/post/1676583/


All Articles