1 Oauth2.0

在这里插入图片描述
步骤:

1 请求授权服务获取授权code。
2 用授权code请求token。
3 用token请求用户信息。

ps:oauth2.0 详情不懂的可以自己百度,也可以去b站学习。

2 google 授权配置

2.1 配置网站

https://console.cloud.google.com/

2.2 配置投屏

在这里插入图片描述

2.3 凭据

在这里插入图片描述

3 JAVA 代码

3.1 pom

 <dependency>
            <groupId>com.google.auth</groupId>
            <artifactId>google-auth-library-oauth2-http</artifactId>
            <version>0.22.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client</artifactId>
            <version>1.32.1</version>
        </dependency>

3.2 授权码回调接口

@RestController
public class GoogleController {


    @GetMapping("/code")
    public String getCode(HttpServletRequest request) {

        return request.getParameter("code");
    }

}

3.3 获取授权码

描述: 运行后,直接访问,获取授权码

public static void main(String[] args) throws GeneralSecurityException, IOException {


        GoogleClientSecrets clientSecrets = new GoogleClientSecrets();
        GoogleClientSecrets.Details details = new GoogleClientSecrets.Details();
        details.setClientId("############");
        details.setClientSecret("###################");
        clientSecrets.setInstalled(details);


        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

        List<String> scopes = Arrays.asList("https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile", "openid");

        // 创建验证流程对象
        GoogleAuthorizationCodeFlow googleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow
                .Builder(httpTransport, jsonFactory, clientSecrets, scopes)
                // AccessType为离线offline,才能获得Refresh Token
                .setAccessType("offline").build();
        // 返回跳转登录请求
        String uri = googleAuthorizationCodeFlow.newAuthorizationUrl().setRedirectUri("http://localhost:8080/code").build();


        System.out.println(uri);

    }

在这里插入图片描述
在这里插入图片描述

3.4 根据授权码获取用户信息


    public static void main(String[] args) throws GeneralSecurityException, IOException {

        GoogleClientSecrets clientSecrets = new GoogleClientSecrets();
        GoogleClientSecrets.Details details = new GoogleClientSecrets.Details();
        details.setClientId("########################");
        details.setClientSecret("############");
        clientSecrets.setInstalled(details);


        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        // 创建验证流程对象

        List<String> scopes = Arrays.asList("https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile", "openid");

        GoogleAuthorizationCodeFlow googleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, clientSecrets, scopes)
                // AccessType为离线offline,才能获得Refresh Token
                .setAccessType("offline").build();

        GoogleAuthorizationCodeTokenRequest tokenRequest = googleAuthorizationCodeFlow.newTokenRequest("4/0AX4XfWh225UarpB-nVkCbOdx5kbPvhjHOlN9t2UgEi2ljlw_QWiaPGLOxx6tzyMI8Ac1rA");
        tokenRequest.setRedirectUri("http://localhost:8080/code");
        tokenRequest.setGrantType("authorization_code");
        GoogleTokenResponse tokenResponse = tokenRequest.execute();
        String token = tokenResponse.getAccessToken();

        if (StringUtils.isNotBlank(tokenResponse.getIdToken())) {
            GoogleIdTokenVerifier idTokenVerifier = new GoogleIdTokenVerifier.Builder(googleAuthorizationCodeFlow.getTransport(), googleAuthorizationCodeFlow.getJsonFactory()).build();
            idTokenVerifier.verify(tokenResponse.getIdToken());
            GoogleIdToken googleIdToken = idTokenVerifier.verify(tokenResponse.getIdToken());
            if (googleIdToken != null && googleIdToken.getPayload() != null) {
                googleIdToken.getPayload().getEmail();

                System.out.println(googleIdToken.getPayload().toString());

            }
        }


    }

在这里插入图片描述

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐