在 google oauth2 中成功登录时出现重定向错误

Getting redirection error while successfully login in google oauth2

我正在使用 spring 安全性来实现 google oauth2 客户端并在成功登录后,同时 google 授权服务器正在重定向获取页面未正确重定向。

@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 控制台

Application.yml

浏览器错误

默认情况下,基本重定向 URI 为:/oauth2/code/{registrationId}

所以你的重定向 uri 应该是:http://localhost:8090/login/oauth2/code/google

如果你想改变它,你可以在 redirectionEndpoint 上做 例如

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