简体   繁体   English

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

[英]Get User Details After Social Login via OAuth2

I'm developing a spring boot app and I have an authantication step.I'm using spring security and also spring-boot-starter-oauth2-client.我正在开发一个 spring 启动应用程序,并且我有一个身份验证步骤。我正在使用 spring 安全性和 spring-boot-starter-oauth2-client。 I want my users can login with google.我希望我的用户可以使用谷歌登录。

I've read a lot of articles about social login with oauth2, they are making siple configurations and its working.Whenever I tried the same steps it did not work for me.我已经阅读了很多关于使用 oauth2 进行社交登录的文章,它们正在进行简单的配置及其工作。每当我尝试相同的步骤时,它对我都不起作用。 I thing I'm missing little point.我觉得我错过了一点。

He is all have done;他已经完成了;

1 - In application.yml I put below conf. 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 - This is my WebSecurityConfigurerAdapter 2 - 这是我的 WebSecurityConfigurerAdapter

@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 - This is my sapmle rest resource 3 - 这是我的样品 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";
    }
}

4 - This my redirectURL conf. 4 - 这是我的redirectURL conf。 on my google account在我的谷歌帐户上

http://localhost:8090/callback

It works up to redirecting to callback rest api, in callback I can get "code" field value but noting more.My question is, How can I get user details like name,email.. How should my callback method body be?它最多可以重定向到回调 rest api,在回调中我可以获得“代码”字段值,但要注意更多。我的问题是,如何获取用户详细信息,例如名称,Z0C83F57C786A0B4A39EFAB23C731 回调方法应该如何?

NOTE: I tried setting注意:我尝试设置

.oauth2Login().successHandler(oAuth2AuthenticationSuccessHandler)

or或者

.oauth2Login().defaultSuccessUrl("/loginSuccess")

in my HttpSecurity configuration but not even triggered在我的 HttpSecurity 配置中,但甚至没有触发

According to RFC 6749 - section 4.1.根据RFC 6749 - 第 4.1 节。 Authorization Code Grant this flow (Authorization Code Grant, that is implemented by Spring Security) you should redirect from authorization server to token endpoint of your provider (google) with proper Authorization code.授权代码授予此流程(授权代码授予,由 Spring Security 实现)您应该使用正确的授权代码从授权服务器重定向到您的提供商(谷歌)的令牌端点。

     +----------+
     | 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)

This endpoint template for Spring Security is: Spring Security 的此端点模板是:
{baseUrl}/login/oauth2/code/{registrationId}
Which for you will be:对你来说,这将是:
http://localhost:8090/login/oauth2/code/google/ (assuming 8090 is a port for your Spring Boot app) http://localhost:8090/login/oauth2/code/google/ (假设 8090 是您的 Spring 启动应用程序的端口)

Code will be automatically added by Spring.代码将由 Spring 自动添加。 After correct validation you will be redirected again and you client (your app) will have access to scope that you requested for.正确验证后,您将再次被重定向,您的客户端(您的应用程序)将有权访问您请求的 scope。 You should be able to see this using:您应该能够使用以下方法看到这一点:

SecurityContextHolder.getContext().getAuthentication().getPrincipal();

I hope this helps.我希望这有帮助。 I know that oauth login can be a pain so good luck and don't give up;-).我知道 oauth 登录可能会很痛苦,所以祝你好运,不要放弃;-)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM