栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 集成 Google 登录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 集成 Google 登录

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
 
            com.google.auth
            google-auth-library-oauth2-http
            0.22.2
        
        
            com.google.api-client
            google-api-client
            1.32.1
        
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 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 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());

            }
        }


    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/956204.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号