我正在构建受OAuth2保护的and服务和一个客户端。对于webservice,我使用了spring安全实现和used this as example。对于客户端,我尝试使用apache oltu库。下面是我的代码片段:
OAuthClientRequest request = OAuthClientRequest.tokenLocation
("http://localhost:8080/oauth/token")
.setGrantType(GrantType.CLIENT_CREDENTIALS)
.setClientId("clientapp")
.setClientSecret("123456")
.buildHeaderMessage();
OAuthAccessTokenResponse oAuthResponse = cli.accessToken(request);
System.out.println(oAuthResponse.getAccessToken());它不起作用。虽然这是
curl -X POST -vu clientapp:123456 --data "grant_type=client_credentials&client_secret=123456&client_id=clientapp" http://localhost:8080/oauth/token效果非常好。下面是curl请求:
POST /oauth/token HTTP/1.1
Authorization: Basic Y2xpZW50YXBwOjEyMzQ1Ng==
User-Agent: curl/7.35.0
Host: localhost:8080
Accept: */*
Content-Length: 70
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&client_secret=123456&client_id=clientapp正如您所看到的,我对curl使用了基本身份验证,并且它起作用了(尽管建议的身份验证类型是持有者)。
下面是oltu数据包:
POST /oauth/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer client_credentials123456clientapp
User-Agent: Java/1.8.0_51
Host: localhost:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 4
null我也不确定持有者授权是如何工作的,但这个包看起来完全错了。
我还尝试使用buildBodyMessage()和buildQueryMessage(),而不是this post中建议的buildHeaderessage(),但也不太好。
发布于 2016-03-08 08:16:50
这行看起来不太健康:
Authorization: Bearer client_credentials123456clientapp我用Oltu创建了一个测试服务器,基本上是一个servlet:
OAuthResponse oauthResponse = OAuthASResponse
.tokenResponse(HttpServletResponse.SC_OK)
.setAccessToken(accessToken)
.setExpiresIn(Integer.toString(expires))
.setRefreshToken(refreshToken)
.buildJSONMessage();
response.setStatus(oauthResponse.getResponseStatus());
response.setContentType("application/json");对于我得到的客户:
OAuthClientRequest request = OAuthClientRequest
.tokenLocation("http://localhost:8083/OltuServer/token")
.setGrantType(GrantType.CLIENT_CREDENTIALS)
.setClientId("clientapp")
.setClientSecret("123456")
.buildQueryMessage();
OAuthAccessTokenResponse oAuthResponse = oAuthClient.accessToken(request);
System.out.println(oAuthResponse.getAccessToken()); 与您的代码的主要区别是buildQueryMessage()。使用buildHeaderMessage(),我在服务器上得到一个异常
OAuthProblemException {error='invalid_request', description='Missing grant_type parameter value' ... }但是我看到Oltu现在是1.0.1版本,而我一直在1.0.0上测试。这个版本可能会有不同的表现。
发布于 2016-03-01 18:16:52
以下内容似乎对我很有效:
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthClientRequest bearerClientRequest = OAuthClientRequest.tokenLocation("http://localhost/rest/auth")
.setUsername("userid")
.setPassword("Password01")
.buildQueryMessage();
bearerClientRequest.setHeader(OAuth.HeaderType.CONTENT_TYPE, "multipart/form-data");
OAuthResourceResponse resourceResponse = oAuthClient.resource(bearerClientRequest, OAuth.HttpMethod.POST, OAuthResourceResponse.class);https://stackoverflow.com/questions/33855176
复制相似问题