繁体   English   中英

一步一步谷歌SSO(java)?

[英]Step by step Google SSO (java)?

我在所有打开的浏览器标签中迷失了Google单点登录:)

我已经有一个应用程序,我想把它放在谷歌市场上。 强制整合是Google SSO。 我用Spring在Struts2上构建了应用程序。

所以现在我需要一些指导如何进行这种集成。 一个例子是完美的。 或者如何开始,使用哪种技术,最佳方法,类似的东西......

另外, 我是否必须使用Google App Engine进行SSO集成? 老实说,我很困惑:)

编辑

我从这里开始: developers.google.com/google-apps/marketplace/sso因为我使用Java,如果你看一下底部的入门,我想使用step2,但链接已经死了。 从那以后我被卡住了......

这里的链接也已死亡。

我使用Apache的HttpClient。 这是我的解决方案,对我来说非常适合。

首先指向授权网址:

https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/userinfo.email&state=%2Fprofile&response_type=code&client_id=<YOUR_CLIENT_ID>&redirect_uri=<YOUR_CALLBACK_URL>

然后从redirect_uri和构建请求主体获取参数以获取access_token

    String code =  request.getParameter("code");
    String foros = "code="+code +
                "&client_id=<YOUR_CLIENT_ID>" +
                "&client_secret=<YOUR_CLIENT_SECRET>" +
                "&redirect_uri="+getText("google.auth.redirect.uri") +
                "&grant_type=authorization_code";

然后使用HttpClient进行POST并使用简单的JSON解析器解析访问令牌。

    HttpClient client = new HttpClient();
    String url = "https://accounts.google.com/o/oauth2/token";
    PostMethod post = new PostMethod(url);
    post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    try {
        post.setRequestEntity(new StringRequestEntity(foros, null, null));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    String accessToken = null;
    try {
        client.executeMethod(post);
        String resp = post.getResponseBodyAsString();
        JSONParser jsonParser = new JSONParser();
        Object obj = jsonParser.parse(resp);
        JSONObject parsed = (JSONObject)obj;            
        accessToken = (String) parsed.get("access_token");
    } catch (HttpException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }

现在您拥有访问令牌,现在可以访问所有 Google API。 例如,获取所有用户信息:

    GetMethod getUserInfo = new GetMethod("https://www.googleapis.com/oauth2/v1/userinfo?access_token="+accessToken);

    String googleId = null;
    String email = null;
    String name = null;
    String firstName = null;
    String lastName = null;
    try {
        client.executeMethod(getUserInfo);
        String resp = getUserInfo.getResponseBodyAsString();
        JSONParser jsonParser = new JSONParser();
        Object obj = jsonParser.parse(resp);
        JSONObject parsed = (JSONObject)obj;
        googleId = (String) parsed.get("id");
        email = (String) parsed.get("email");
        name = (String) parsed.get("name");
        firstName = (String) parsed.get("given_name");
        lastName = (String) parsed.get("family_name");
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }

您现在可以保存此数据并将其用于登录您的应用程序。

您可以按照Google Apps Marketplace的Java教程来解释如何执行单点登录: https//developers.google.com/google-apps/marketplace/tutorial_java

该教程还包括一个带有应用程序源和所有必需库的zip的下载链接,包括第2步: http//apps-marketplace-resources.googlecode.com/files/helloworld-java-2011050303.zip

以下是文档中已损坏的有效链接:

暂无
暂无

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

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