Spring Boot, OAuth2 with azure and another provider (e.g. google)

1k views Asked by At

I have an application that is using Azure Active directory to authenticate and I need to add another provider, for example google.

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends AadWebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
    super.configure(http);
      http.authorizeRequests()
              .anyRequest().authenticated();
    }
}


spring:
  cloud:
    azure:
      active-directory:
        enabled: true
        profile:
          tenant-id: 
#        credential:
          client-id: 
          client-secret: 
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: 
            client-secret: 

Using above code will force azure login.

How can I adapt the code to have both options azure and google?

1

There are 1 answers

0
telebog On BEST ANSWER

I did not manage to make it work with spring-cloud-azure-starter-active-directory, so I removed this plugin and used:

 spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: 
            client-secret:
          azure:
            client-id: 
            client-secret: 
            scope:
              - openid
              - profile
              - email
            authorization-grant-type: authorization_code
            redirect-uri: http://localhost:8080/login/oauth2/code/azure
            provider: azure-active-directory
        provider:
          azure-active-directory:
            issuer-uri: https://login.microsoftonline.com/{tenant-id}/v2.0

This example helped me https://github.com/Azure-Samples/azure-spring-boot-samples/blob/spring-cloud-azure_v4.4.1/aad/spring-security/servlet/oauth2/login/src/main/resources/application.yml