Java 通过OAuth2进行社交登录后获取用户详细信息

Java 通过OAuth2进行社交登录后获取用户详细信息,java,spring-boot,spring-security,spring-security-oauth2,Java,Spring Boot,Spring Security,Spring Security Oauth2,我正在开发一个SpringBoot应用程序,我有一个验证步骤。我使用SpringSecurity和spring-boot-starter-oauth2-client。我希望我的用户可以登录谷歌 我读过很多关于oauth2社交登录的文章,他们正在进行siple配置,并且可以正常工作。每当我尝试同样的步骤时,它对我都不起作用。我觉得我遗漏了一点 他是我所做的一切 1-在application.yml中,我将其放在conf下面 spring: security: oauth2:

我正在开发一个SpringBoot应用程序,我有一个验证步骤。我使用SpringSecurity和spring-boot-starter-oauth2-client。我希望我的用户可以登录谷歌

我读过很多关于oauth2社交登录的文章,他们正在进行siple配置,并且可以正常工作。每当我尝试同样的步骤时,它对我都不起作用。我觉得我遗漏了一点

他是我所做的一切

1-在application.yml中,我将其放在conf下面

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            clientId: 876826483277-eap24vioi12cp4bjld5bqr8hir0t5kfl.apps.googleusercontent.com
            clientSecret: bm3HkBDhYmycEnRwAFbR1-mL
            redirectUri: http://localhost:8090/callback
            scope:
              - email
              - profile
2-这是我的Web安全配置适配器

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http
            .csrf()
            .disable()
            .authorizeRequests()
            .antMatchers("/callback/**").permitAll()
            .anyRequest().authenticated()
            .and()
        .oauth2Login();
        
        // @formatter:on
    }
}
3-这是我的sapmle rest资源

@RestController
public class OAuth2Resource {

    @RequestMapping(method = RequestMethod.GET, value = "/callback")
    public String callback(@RequestParam Map<String, String> requestParamMap) {
        System.out.println("Code = " + requestParamMap.get("code"));
        return "OK";
    }
}
它可以重定向到回调rest api,在回调中,我可以得到“code”字段值,但需要注意的是更多。我的问题是,如何获得用户名、电子邮件等用户详细信息。。我的回调方法体应该是怎样的

注意:我试过设置

.oauth2Login().successHandler(oAuth2AuthenticationSuccessHandler)

在我的HttpSecurity配置中,但根据此流(授权码授权,由Spring Security实现),您应该使用正确的授权码从授权服务器重定向到提供商(google)的令牌端点

     +----------+
     | Resource |
     |   Owner  |
     |          |
     +----------+
          ^
          |
         (B)
     +----|-----+          Client Identifier      +---------------+
     |         -+----(A)-- & Redirection URI ---->|               |
     |  User-   |                                 | Authorization |
     |  Agent  -+----(B)-- User authenticates --->|     Server    |
     |          |                                 |               |
     |         -+----(C)-- Authorization Code ---<|               |
     +-|----|---+                                 +---------------+
       |    |                                         ^      v
      (A)  (C)                                        |      |
       |    |                                         |      |
       ^    v                                         |      |
     +---------+                                      |      |
     |         |>---(D)-- Authorization Code ---------'      |
     |  Client |          & Redirection URI                  |
     |         |                                             |
     |         |<---(E)----- Access Token -------------------'
     +---------+       (w/ Optional Refresh Token)
我希望这有帮助。我知道oauth登录会很痛苦,所以祝你好运,不要放弃;-)

.oauth2Login().defaultSuccessUrl("/loginSuccess")
     +----------+
     | Resource |
     |   Owner  |
     |          |
     +----------+
          ^
          |
         (B)
     +----|-----+          Client Identifier      +---------------+
     |         -+----(A)-- & Redirection URI ---->|               |
     |  User-   |                                 | Authorization |
     |  Agent  -+----(B)-- User authenticates --->|     Server    |
     |          |                                 |               |
     |         -+----(C)-- Authorization Code ---<|               |
     +-|----|---+                                 +---------------+
       |    |                                         ^      v
      (A)  (C)                                        |      |
       |    |                                         |      |
       ^    v                                         |      |
     +---------+                                      |      |
     |         |>---(D)-- Authorization Code ---------'      |
     |  Client |          & Redirection URI                  |
     |         |                                             |
     |         |<---(E)----- Access Token -------------------'
     +---------+       (w/ Optional Refresh Token)
SecurityContextHolder.getContext().getAuthentication().getPrincipal();