【问题标题】:Oauth2 Authentication handle code programmaticallyOauth2 身份验证以编程方式处理代码
【发布时间】:2019-05-09 12:36:08
【问题描述】:
我使用 Apache OLTU 库实现了 OAuth2 身份验证。
它可以工作,但我手动处理来自 RedirectURL 的请求令牌。
-
步骤:
request = OAuthClientRequest.authorizationProvider(OAuthProviderType.GOOGLE)//authorizationProvider(OAuthProviderType.GOOGLE)
.setState(OAuth.OAUTH_STATE)
.setResponseType(OAuth.OAUTH_CODE)
.setRedirectURI("http://localhost:8080")
.setClientId(clientId)
.setScope("https://www.googleapis.com/auth/drive")
.buildQueryMessage();
-
步骤:
OAuthClientRequest oAuthClientRequest = OAuthClientRequest.tokenProvider(OAuthProviderType.GOOGLE)
.setGrantType(GrantType.AUTHORIZATION_CODE)
.setClientId(clientId)
.setClientSecret(clientSecret)
.setRedirectURI("http://localhost:8080")
.setCode(requestCode).buildBodyMessage();
在这两步之间,我需要自动处理代码的提取。
这一步如何在代码中实现?
我不会在 servlet 中,而是在 Portlet 中。
【问题讨论】:
标签:
authentication
oauth
oauth-2.0
oltu
【解决方案1】:
问题“如何在代码中实现这一步?”
要求“我不会在 servlet 中,而是在 Portlet 中。”
回答:
(1) 为了您的参考,我将源代码添加到您的源代码中(使用 Apache OLTU 库)以“自动处理 OAuth 授权代码的提取”。
// 1. Step
OAuthClientRequest request = OAuthClientRequest.authorizationProvider(OAuthProviderType.GOOGLE)//authorizationProvider(OAuthProviderType.GOOGLE) .setState(OAuth.OAUTH_STATE) .setResponseType(OAuth.OAUTH_CODE) .setRedirectURI("http://localhost:8080") .setClientId(clientId) .setScope("https://www.googleapis.com/auth/drive") .buildQueryMessage();
// Create the response wrapper
OAuthAuthzResponse oar = null;
oar = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
// Get Authorization Code
String requestCode = oar.getCode();
// 2. Step
OAuthClientRequest oAuthClientRequest = OAuthClientRequest.tokenProvider(OAuthProviderType.GOOGLE) .setGrantType(GrantType.AUTHORIZATION_CODE) .setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI("http://localhost:8080") .setCode(requestCode).buildBodyMessage();
(2)更多细节可以参考下面的示例代码
"demos/client-demo/src/main/java/org/apache/oltu/oauth2/client/demo/controller/RedirectController.java"
来自 GitHub 存储库的 Apache Oltu OAuth 2.0 Client and Provider,这是 Apache Oltu 的一个分支,带有 Pull Request #10 用于两个新提交“添加提供程序演示和自述文件”。
提供者演示应用程序(GitHub 存储库中Apache Oltu OAuth 2.0 Client and Provider 中的“demos/provider-demo”)允许您运行独立的 OAuth 2.0 服务器来测试和调试 OAuth2 身份验证客户端(由您在 Portlet 中实现)。