如何在我的网站上实现“使用谷歌登录”?

时间:2013-08-04 07:27:25

标签: java-ee web-applications oauth oauth-2.0 scribe

我想在基于JSP和Servlets的J2EE Web应用程序中实现“使用Google登录”类型登录。如何实现它。

让我解释一下::

  

1)用户点击“使用谷歌登录”。
  2)现在,他重定向到Google的页面并点击“允许访问”。
  3)单击允许访问后,他重定向到他的用户门户

这是程序。我使用了Scribe APi,但我不知道如何在jsp页面中实现它。

您能解释一下如何使用任何开源API实现这一点吗?

  

(我搜索了StackOVerflow,但我只找到了php / asp但没有找到jsp)

请不要关闭我的问题,因为我是stackoverflow的新手。

1 个答案:

答案 0 :(得分:0)

示例Java代码

// Create a state token to prevent request forgery.
  // Store it in the session for later validation.
  String state = new BigInteger(130, new SecureRandom()).toString(32);
  request.session().attribute("state", state);
  // Read index.html into memory, and set the Client ID,
  // Token State, and Application Name in the HTML before serving it.
  return new Scanner(new File("index.html"), "UTF-8")
      .useDelimiter("\\A").next()
      .replaceAll("[{]{2}\\s*CLIENT_ID\\s*[}]{2}", CLIENT_ID)
      .replaceAll("[{]{2}\\s*STATE\\s*[}]{2}", state)
      .replaceAll("[{]{2}\\s*APPLICATION_NAME\\s*[}]{2}",
                  APPLICATION_NAME);

它以Java形式提供,就流程而言,为了完整答案,我想简要介绍一下。

  1. 用户点击“使用Google登录”
  2. 由于使用了javascript,用户被重定向到用户所在的某个google页面 要求许可。
  3. 在接受或授予权限时,Google会重定向用户(将接受/未接受的响应和应用程序请求的其他用户相关数据)发送到注册应用时指定的某个预先确定的URL。
  4. 然后,url处理程序(servlet / jsp)负责获取谷歌响应请求的数据并将其保存在数据库中
  5. 希望能够明确解释。您可以在LRA的问题评论中提到的链接中轻松找到更多信息。

相关问题