方法抛出“javax.net.ssl.SSLException”异常

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

我编写了一个 JUnit 来测试下面调用 getAuthToken 的方法之一

public String getAuthToken() throws URISyntaxException, IOException, InterruptedException {
    String formBody = String.format("grant_type=client_credentials&client_id=%s&client_secret=%s&scope=%s",
            URLEncoder.encode(clientId, StandardCharsets.UTF_8),
            URLEncoder.encode(clientSecret, StandardCharsets.UTF_8),
            URLEncoder.encode(scope, StandardCharsets.UTF_8));

    HttpRequest authRequest = HttpRequest.newBuilder()
            .uri(new URI(epimTokenUri))
            .header("Content-Type", "application/x-www-form-urlencoded")
            .POST(HttpRequest.BodyPublishers.ofString(formBody))
            .build();

    HttpResponse<String> authResponse = client.send(authRequest, HttpResponse.BodyHandlers.ofString());

    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode jsonNode = objectMapper.readTree(authResponse.body());

    return jsonNode.get("access_token").asText();
}

为此,我使用 WireMock。以下是 WireMock 获取 token 的映射

{
  "request": {
    "method": "POST",
    "url": "/.default/token"
  },
  "response": {
    "status": 200,
    "jsonBody": {
      "access_token": "testToken",
      "expires_in": 3600,
      "refresh_expires_in": 0,
      "token_type": "Bearer",
      "not-before-policy": 0,
      "scope": "https://localhost:9082/.default"
    },
    "headers": {
      "Content-Type": "application/x-www-form-urlencoded"
    }
  }
}

这会产生错误,因为方法抛出了“javax.net.ssl.SSLException”异常。在线上

HttpResponse<String> authResponse = client.send(authRequest, HttpResponse.BodyHandlers.ofString());

有人可以帮我解决这个问题吗?这里出了什么问题或者有其他方法来模拟这个方法以在我的 JUnit 中使用?以下是完整的 stackTrace

java http ssl junit wiremock
1个回答
0
投票

在我的 test.yaml 文件中,tokenURI 带有 https,因此它需要我没有通过wiremock 提供的证书。换成http,就通过了。解决此问题的另一种方法是使用证书设置wiremock以接受ssl。

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