javaspringspring-security-oauth2google-client

Getting redirection error while successfully login in google oauth2


I am using spring security to implement google oauth2 client and after successful login, while google authorization server is redirecting getting page isn't redirecting properly.

@Configuration
@EnableWebSecurity
@EnableOAuth2Client
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .oauth2Login()
            .failureUrl("/login?error")
            .permitAll()
            .and()
            .logout()
            .logoutSuccessUrl("http://www.google.com")
            .and()
            .oauth2Client();
    }
}

# oauth2 client configuration
spring:
 security:
  oauth2:
    client:
     registration:
      google:
       provider: google
        clientId: 
         client-secret: 
          authorization-grant-type: authorization_code
            redirect-uri: http://localhost:8090/home
              scope: openid,profile,email
      provider:
       google:
        authorization-uri: https://accounts.google.com/o/oauth2/v2/auth
        token-uri: https://oauth2.googleapis.com/token
        user-info-uri: https://openidconnect.googleapis.com/v1/userinfo
        user-name-attribute: sub 
        jwk-set-uri: https://www.googleapis.com/oauth2/v3/certs
        
server:
  port: 8090
    

google console

google console

Application.yml

Application.yml

browser error

browser error


Solution

  • By default the base redirect URI is: /oauth2/code/{registrationId}

    So your redirect uri should be: http://localhost:8090/login/oauth2/code/google

    if you want to change it, you can do that on the redirectionEndpoint e.g.

    .oauth2Login()
      .redirectionEndpoint()
      .baseUri("your custom redirect uri")