一步一步谷歌SSO(java)?

Tri*_*ick 13 java single-sign-on oauth-2.0

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

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

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

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

编辑

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

这里的链接也已死亡.

Tri*_*ick 8

我使用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";
Run Code Online (Sandbox Code Playgroud)

然后使用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);
    }
Run Code Online (Sandbox Code Playgroud)

现在您拥有访问令牌,现在可以访问所有 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);
    }
Run Code Online (Sandbox Code Playgroud)

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


Cla*_*ino 5

您可以按照 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

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