OAuth2 oltu客户端

时间:2015-11-22 13:16:41

标签: java oauth-2.0 authorization bearer-token oltu

我正在构建我的oauth2-protecet webservice和一个客户端。对于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

效果很好。这是卷曲请求:

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()而不是this post中提到的buildHeaderessage(),但它也没有用。

2 个答案:

答案 0 :(得分:1)

这条线看起来不太健康:

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上测试了。该版本可能表现不同。

答案 1 :(得分:0)

以下似乎对我有用:

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);