Vertx 3.6.3:无法使用PFX选项启动HTTPS服务器

时间:2019-02-26 15:43:59

标签: java https vert.x pfx

我正在使用Vertx 3.6.3。我正在尝试运行HTTPS服务器Verticle,但是不幸的是,Verticle没有得到部署。您能告诉我我在哪里做错了吗?

这是我的Verticle:

HTTPSVerticle:

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.PfxOptions;

public class HTTPSVerticle extends AbstractVerticle {
    @Override
    public void start(Future<Void> httpsServerStarted) throws Exception {
        int port = config().getJsonObject("http", new JsonObject()).getInteger("port", 8000);
        boolean useSsl = config().getJsonObject("http", new JsonObject()).getBoolean("useSsl", false);
        String sslCertPath = config().getJsonObject("http", new JsonObject()).getString("sslCertPath", "");
        String sslCertPassword = config().getJsonObject("http", new JsonObject()).getString("sslCertPassword", "");

        HttpServerOptions httpServerOptions = new HttpServerOptions();

        System.out.println(useSsl);

        if (useSsl)
            httpServerOptions
                    .setSsl(true)
                    //.setClientAuth(ClientAuth.REQUIRED)
                    .setPfxTrustOptions(
                            new PfxOptions().setPath(sslCertPath).setPassword(sslCertPassword)
                    );

        vertx.createHttpServer(httpServerOptions).requestHandler(httpReq -> {
            httpReq.response().end("Hello encrypted world");
        }).listen(port, fut -> {
            if (fut.succeeded()) {
                System.out.println("Verticle now listening on port: " + port);
                httpsServerStarted.complete();
            }
            else {
                httpsServerStarted.fail(fut.cause());
                System.out.println("Error while starting HTTP server");
            }
        });
    }
}

这是我的测试用例:

TestHTTPSVerticle:

import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(VertxUnitRunner.class)
public class TestHTTPSVerticle {
    private static Vertx vertx;

    @BeforeClass
    public static void setUp(TestContext context) {
        DeploymentOptions opts = new DeploymentOptions()
                .setConfig(new JsonObject().put("http", new JsonObject()
                        .put("useSsl", true)
                        .put("sslCertPath", "test.pfx")
                        .put("sslCertPassword", "abcd")));

        vertx = Vertx.vertx();
        vertx.deployVerticle(HTTPSVerticle.class.getName(), opts, context.asyncAssertSuccess());
    }

    @AfterClass
    public static void tearDown(TestContext context) {
        vertx.close(context.asyncAssertSuccess());
    }

    @Test
    public void testHttpsServerMessage(TestContext context) {
        Async async = context.async();

        System.out.println("Connecting to server...");

        vertx.createHttpClient().get(8000, "localhost", "/loremipsum", respHandler -> respHandler.bodyHandler(respBody -> {
            System.out.println(respBody);
            context.assertTrue(respBody.toString().equals("Hello encrypted world"));
            async.complete();
        })).end();
    }
}

不允许我不加说明地提交它,因此重复进行以下说明:

  1. 我正在使用vertx配置机制来获取portuseSslsslCertPathsslCertPassword
  2. 我正在使用HttpServerOptions为http服务器配置SSL设置
  3. 服务器成功启动后,它应该打印Verticle now listening on port: 8000
  4. 万一服务器无法启动,它应该打印Error while starting HTTP server

但是,它永远不会使用 AsyncResult 调用侦听器的处理程序。

0 个答案:

没有答案