我正在构建我的 oauth2-protecet 网络服务和一个客户端。对于 web 服务,我使用了 spring 安全实现,并将其用作示例。对于客户,我正在尝试 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
效果很好。这是卷曲请求:
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()
andbuildQueryMessage()
而不是这篇文章buildHeaderessage()
中建议的,但这也不好。