在 OAuth2RestTemplate 中传递 cookie

Pass cookie in OAuth2RestTemplate

我正在使用 Spring 引导 1.5.3 和 Java 8。我正在使用 Oauth2RestClient 从一个休息端点调用远程休息端点。调用成功,但我无法在请求中传递 OAUTH-ACCESS-TOKEN cookie。您能否指导我如何在 oauth2RestTemplate 中传递此 cookie。

    private HttpEntity<T> getHttpEntity(OAuth2RestClientDetails oAuth2RestClientDetails, T requestData) {
        HttpEntity<T> httpEntity = null;
        HttpHeaders httpHeaders = getHttpHeaders(oAuth2RestClientDetails);
        if (requestData != null && httpHeaders.size() > 0) {
            httpEntity = new HttpEntity<T>(requestData, httpHeaders);
        } else if (requestData != null) {
            httpEntity = new HttpEntity<T>(requestData);
        }
        return httpEntity;
    }

问题已解决

以下部分代码有问题:

private HttpEntity<T> getHttpEntity(OAuth2RestClientDetails 
  oAuth2RestClientDetails, T requestData) {
    HttpEntity<T> httpEntity = null;
    HttpHeaders httpHeaders = getHttpHeaders(oAuth2RestClientDetails);
    if (requestData != null && httpHeaders.size() > 0) {
        httpEntity = new HttpEntity<T>(requestData, httpHeaders);
    } else if (requestData != null) {
        httpEntity = new HttpEntity<T>(requestData);
    }
    return httpEntity;
}

它有条件地发送 httpHeaders,即当 requestData 不为空且 header 大小大于零时。通过纠正这个逻辑,问题就解决了。

谢谢。

This is a Community Wiki answer replacing the answer having been edited into the original question as recommended by Meta. The solution was posted by the asker initially.

下面的代码部分有问题:

private HttpEntity<T> getHttpEntity(OAuth2RestClientDetails 
  oAuth2RestClientDetails, T requestData) {
    HttpEntity<T> httpEntity = null;
    HttpHeaders httpHeaders = getHttpHeaders(oAuth2RestClientDetails);
    if (requestData != null && httpHeaders.size() > 0) {
        httpEntity = new HttpEntity<T>(requestData, httpHeaders);
    } else if (requestData != null) {
        httpEntity = new HttpEntity<T>(requestData);
    }
    return httpEntity;
}

它有条件地发送 httpHeaders,即当 requestData 不为空且 header 大小大于零时。通过纠正这个逻辑,问题就解决了。