【问题标题】:MockServer How to implement a JUnit mutual TLS authentication?MockServer 如何实现 JUnit 双向 TLS 身份验证?
【发布时间】:2021-03-05 16:10:12
【问题描述】:

我的 SpringBoot 应用程序正在使用第三个库/框架连接到第三方服务器。 这些远程服务器之一(尚不可用)将需要双向 TLS (https) 连接。我的代码将为框架提供 P12 和密码,以便连接到这个远程服务器。我需要使用 Junit 集成测试来测试这部分。

MockServer(来自www.mock-server.com)已经用于我们的 Junit IT,但还没有用于双向 TLS 身份验证,作为新手,我对如何实现这一点感到困惑。 形成文档 (https://www.mock-server.com/mock_server/HTTPS_TLS.html) 我在 Junit 测试中添加了以下配置:

@BeforeAll
public static void initServer(){

  //ensure https will use SSL context defined by MockServer to allow
  // dynamically generated certificates to be accepted
  HttpsURLConnection.setDefaultSSLSocketFactory(new KeyStoreFactory(new 
  MockServerLogger()).sslContext().getSocketFactory());

  File certif = ResourceUtils.getFile("classpath:mtls/test-cert.crt");
  ConfigurationProperties.tlsMutualAuthenticationCertificateChain(certif.getPath());
  ConfigurationProperties.tlsMutualAuthenticationRequired(true);

  mockServer = ClientAndServer.startClientAndServer(80,443);

  etc…
}

但没有任何效果,我的测试中缺少一些代码
在日志中我可以看到即:

"MockServerEventLog - no tls for connection"

我找不到任何关于这个用例的清晰简单的教程。任何帮助都会很棒。

那么如何使用具有双向 TLS 身份验证的 Mockserver 实现完整的 Junit 集成测试?

【问题讨论】:

  • 您找到解决方案了吗?我有同样的问题..
  • 有人找到解决办法了吗?

标签: java junit ssl-certificate tls1.2 mockserver


【解决方案1】:

遇到这个问题,我在创建期望时添加了.withSecure(true)

@Test
public void myTest() {
    mockServer
        .withSecure(true)
        .when(request()
            .withPath("/test"))
        .respond(response()
            .withBody("hello world"));
}

【讨论】:

    猜你喜欢
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 2020-09-09
    • 2022-08-23
    • 1970-01-01
    • 2020-09-05
    • 2020-12-05
    相关资源
    最近更新 更多