您好,我从 java 11 从
RestTemplate
切换到 HttpClient
。我也想从 java 11 从 OAuth2RestTemplate
切换到 HttpClient
。
我找不到任何有关将
HttpClient
与 OAuth2 结合使用的材料。可以用简单的方法吗?
这是个好主意吗?或者我应该使用春天的
WebClient
?
我刚刚使用
HttpClient
来获取 OAuthToken,然后将令牌添加到下一个请求的标头中。
String tokenRequest =
"client_id="
+ lmGatewayCorrectClientId
+ "&client_secret="
+ lmGatewayCorrectClientSecret
+ "&grant_type="
+ lmGatewayCorrectGrantType;
HttpClient oAuth2Client = HttpClient.newBuilder().build();
HttpRequest oAuth2Request =
HttpRequest.newBuilder()
.header(CONTENT_TYPE, APPLICATION_FORM_URLENCODED_VALUE)
.uri(URI.create(lmGatewayCorrectAccessTokenUri))
.POST(HttpRequest.BodyPublishers.ofString(tokenRequest))
.build();
HttpResponse<String> oAuth2Response = null;
try {
oAuth2Response = oAuth2Client.send(oAuth2Request, HttpResponse.BodyHandlers.ofString());
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
请问您是如何处理刷新令牌逻辑的?