【问题标题】:OAuth2 oltu clientOAuth2 oltu 客户端
【发布时间】: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


    【解决方案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 上进行测试。该版本的行为可能会有所不同。

    【讨论】:

      【解决方案2】:

      以下似乎对我有用:

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

      【讨论】:

      • 您能否详细说明为什么该解决方案对您有效?
      猜你喜欢
      • 2023-03-03
      • 2012-10-22
      • 2015-11-11
      • 2013-06-20
      • 2016-06-26
      • 2021-12-22
      • 2015-11-09
      • 2015-10-31
      • 1970-01-01
      相关资源
      最近更新 更多