java – 使用Google OAuth2 API对Google App用户进行身份验证
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 使用Google OAuth2 API对Google App用户进行身份验证,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2621字,纯文字阅读大概需要4分钟。
内容图文
![java – 使用Google OAuth2 API对Google App用户进行身份验证](/upload/InfoBanner/zyjiaocheng/785/c0c82dc7cb2349d49fdd0e0030063de8.jpg)
我想知道我是否可以使用谷歌客户端API(Java)来验证谷歌应用程序域的用户到我的应用程序.
目标应用程序是使用REST后端(jersey)的Web应用程序.
documentation不是很清楚(或者我误解了它),文档中的示例引用了已弃用的类……有人知道它是否可能并且是最好的方法.
代码示例将不胜感激.
解决方法:
Google Apps帐户应该可以与API一起使用.
唯一的例外是域管理员禁用该服务.例如,如果域管理员禁用了Google功能,您将无法访问该用户的Google数据.
无需更改代码,因此您应该能够使用samples in the client library repository中的任何代码或this one for Google+等产品特定样本.
Google starter项目首先通过在com.google.api.sample.OAuth2AuthorizationCodeServlet中扩展AbstractAuthorizationCodeServlet来实现OAuth流程
public class OAuth2AuthorizationCodeServlet
extends AbstractAuthorizationCodeServlet {
/**
* If the user already has a valid credential held in the
* AuthorizationCodeFlow they are simply returned to the home page.
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("/");
}
/**
* Returns the URI to redirect to with the authentication result.
*/
@Override
protected String getRedirectUri(HttpServletRequest request)
throws ServletException, IOException {
return ConfigHelper.REDIRECT_URI;
}
/**
* Returns the HTTP session id as the identifier for the current user.
* The users credentials are stored against this ID.
*/
@Override
protected String getUserId(HttpServletRequest request)
throws ServletException, IOException {
return request.getSession(true).getId();
}
@Override
protected AuthorizationCodeFlow initializeFlow() throws ServletException,
IOException {
return Util.getFlow();
}
}
然后通过扩展AbstractAuthorizationCodeCallbackServlet完成com.google.api.sample.Oauth2CallbackServlet中的流程:
public class OAuth2CallbackServlet
extends AbstractAuthorizationCodeCallbackServlet {
@Override
protected void onSuccess(HttpServletRequest request,
HttpServletResponse response, Credential credential)
throws ServletException, IOException {
response.sendRedirect("/");
}
@Override
protected void one rror(HttpServletRequest req, HttpServletResponse resp,
AuthorizationCodeResponseUrl errorResponse)
throws ServletException, IOException {
resp.sendError(SC_INTERNAL_SERVER_ERROR, "Something went wrong :(");
}
@Override
protected String getRedirectUri(HttpServletRequest request)
throws ServletException, IOException {
return ConfigHelper.REDIRECT_URI;
}
@Override
protected AuthorizationCodeFlow initializeFlow()
throws IOException {
return Util.getFlow();
}
@Override
protected String getUserId(HttpServletRequest request) throws ServletException, IOException {
return request.getSession(true).getId();
}
}
内容总结
以上是互联网集市为您收集整理的java – 使用Google OAuth2 API对Google App用户进行身份验证全部内容,希望文章能够帮你解决java – 使用Google OAuth2 API对Google App用户进行身份验证所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。