【发布时间】:2016-03-08 08:16:49
【问题描述】:
我正在构建我的 oauth2-protecet 网络服务和一个客户端。对于 web 服务,我使用了 spring 安全实现和used this as example。对于客户端,我正在尝试 apache oltu 库。这是我的sn-p:
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 的基本身份验证并且它有效(即使建议的身份验证类型是 Bearer)。
这是 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() 代替buildHeaderessage(),正如this post 中所建议的那样,但这也不好。
【问题讨论】:
标签: java oauth-2.0 authorization bearer-token oltu