有没有办法将 java.net.http.HttpClient 与 Oauth2 一起使用?

问题描述 投票:0回答:2

您好,我从 java 11 从

RestTemplate
切换到
HttpClient
。我也想从 java 11 从
OAuth2RestTemplate
切换到
HttpClient

我找不到任何有关将

HttpClient
与 OAuth2 结合使用的材料。可以用简单的方法吗?

这是个好主意吗?或者我应该使用春天的

WebClient

java oauth-2.0 httpclient java-http-client
2个回答
8
投票

我刚刚使用

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

0
投票

请问您是如何处理刷新令牌逻辑的?

© www.soinside.com 2019 - 2024. All rights reserved.