Wiremock如何确定要使用的TLS版本?

时间:2019-02-14 05:48:37

标签: java wiremock

我有一个客户端,该客户端通过指定TLS 1.2将握手请求发送到Wiremock,但Wiremock使用TLS 1.0版进行回复。 如何让Wiremock使用1.2版

1 个答案:

答案 0 :(得分:2)

您是否曾经发现过如何使WireMock使用TLS 1.2?

编辑:要设置仅支持TLS 1.2的WireMock,您必须实现自己的HttpServerFactory,以构建自定义HttpServer的实例。 WireMock设置将如下所示:

WireMockServer server = new WireMockServer(
    WireMockConfiguration.options()
        .httpServerFactory(new CustomHttpServerFactory())
        .dynamicHttpsPort()
        .dynamicPort());

原始: 我发现标准的WireMock实现使用JettyHttpServerFactory查找以下类:com.github.tomakehurst.wiremock.jetty94.Jetty94HttpServercom.github.tomakehurst.wiremock.jetty92.Jetty92HttpServer。如果找到其中一个,它将创建该类的实例。

Jetty94HttpStarter的优先级高于Jetty92HttpStarter

我的解决方案是创建Jetty92HttpServer的副本并将其放在项目的com.github.tomakehurst.wiremock.jetty92包中。

package com.github.tomakehurst.wiremock.jetty92;

import com.github.tomakehurst.wiremock.core.Options;
import com.github.tomakehurst.wiremock.http.AdminRequestHandler;
import com.github.tomakehurst.wiremock.http.StubRequestHandler;
import com.github.tomakehurst.wiremock.jetty9.JettyHttpServer;
import com.github.tomakehurst.wiremock.servlet.MultipartRequestConfigurer;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Jetty92HttpServer extends JettyHttpServer {
    private final Logger logger = LoggerFactory.getLogger(JettyStarter.class);

    public Jetty92HttpServer(final Options options, final AdminRequestHandler adminRequestHandler, final StubRequestHandler stubRequestHandler) {
        super(options, adminRequestHandler, stubRequestHandler);
    }

    @Override
    public SslContextFactory buildSslContextFactory() {
        logger.debug("Creates CustomizedSslContextFactory");
        final SslContextFactory factory = new CustomizedSslContextFactory();

        logger.debug("Exclude protocols TLS, TLSv1 and TLSv1.1");
        factory.addExcludeProtocols("TLS", "TLSv1", "TLSv1.1");

        logger.debug("Set protocol=TLSv1.2");
        factory.setProtocol("TLSv1.2");

        return factory;
    }

    @Override
    public MultipartRequestConfigurer buildMultipartRequestConfigurer() {
        return new Jetty92MultipartRequestConfigurer();
    }
}

我知道这适用于Java 7,并且应该适用于Java8。但是,我不确定它是否适用于Java 9及更高版本,但我认为它应该可以正常工作。

我建议以下SO帖子,因为它们包含一些对我有帮助的链接: