Search in sources :

Example 6 with PemKeyCertOptions

use of io.vertx.core.net.PemKeyCertOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    HttpServer server = vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("server-key.pem").setCertPath("server-cert.pem")));
    server.requestHandler(req -> {
        req.response().putHeader("content-type", "text/html").end("<html><body>" + "<h1>Hello from vert.x!</h1>" + "<p>version = " + req.version() + "</p>" + "</body></html>");
    }).listen(8443);
}
Also used : OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Runner(io.vertx.example.util.Runner) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions)

Example 7 with PemKeyCertOptions

use of io.vertx.core.net.PemKeyCertOptions in project VX-API-Gateway by EliMirren.

the class VxApiApplication method createHttpsServer.

/**
 * 创建https服务器
 *
 * @param createHttp
 */
public void createHttpsServer(Handler<AsyncResult<Void>> createHttps) {
    this.httpsRouter = Router.router(vertx);
    httpsRouter.route().handler(this::filterBlackIP);
    httpsRouter.route().handler(CookieHandler.create());
    SessionStore sessionStore = null;
    if (vertx.isClustered()) {
        sessionStore = ClusteredSessionStore.create(vertx);
    } else {
        sessionStore = LocalSessionStore.create(vertx);
    }
    SessionHandler sessionHandler = SessionHandler.create(sessionStore);
    sessionHandler.setSessionCookieName(appOption.getSessionCookieName());
    sessionHandler.setSessionTimeout(appOption.getSessionTimeOut());
    httpsRouter.route().handler(sessionHandler);
    httpsRouter.route().handler(BodyHandler.create().setUploadsDirectory("../temp/file-uploads").setBodyLimit(appOption.getContentLength()));
    // 跨域处理
    if (corsOptions != null) {
        CorsHandler corsHandler = CorsHandler.create(corsOptions.getAllowedOrigin());
        corsHandler.allowedHeaders(corsOptions.getAllowedHeaders()).allowCredentials(corsOptions.isAllowCredentials()).exposedHeaders(corsOptions.getExposedHeaders()).allowedMethods(corsOptions.getAllowedMethods()).maxAgeSeconds(corsOptions.getMaxAgeSeconds());
        httpsRouter.route().handler(corsHandler);
    }
    // 创建https服务器
    serverOptions.setSsl(true);
    VxApiCertOptions certOptions = serverOptions.getCertOptions();
    if (certOptions.getCertType().equalsIgnoreCase("pem")) {
        serverOptions.setPemKeyCertOptions(new PemKeyCertOptions().setCertPath(certOptions.getCertPath()).setKeyPath(certOptions.getCertKey()));
    } else if (certOptions.getCertType().equalsIgnoreCase("pfx")) {
        serverOptions.setPfxKeyCertOptions(new PfxOptions().setPath(certOptions.getCertPath()).setPassword(certOptions.getCertKey()));
    } else {
        LOG.error("创建https服务器-->失败:无效的证书类型,只支持pem/pfx格式的证书");
        createHttps.handle(Future.failedFuture("创建https服务器-->失败:无效的证书类型,只支持pem/pfx格式的证书"));
        return;
    }
    Future<Boolean> createFuture = Future.future();
    vertx.fileSystem().exists(certOptions.getCertPath(), createFuture);
    createFuture.setHandler(check -> {
        if (check.succeeded()) {
            if (check.result()) {
                // 404页面
                httpsRouter.route().order(999999).handler(rct -> {
                    HttpServerResponse response = rct.response();
                    if (appOption.getNotFoundContentType() != null) {
                        response.putHeader("Content-Type", appOption.getNotFoundContentType());
                    }
                    response.end(appOption.getNotFoundResult());
                });
                // 如果在linux系统开启epoll
                if (vertx.isNativeTransportEnabled()) {
                    serverOptions.setTcpFastOpen(true).setTcpCork(true).setTcpQuickAck(true).setReusePort(true);
                }
                vertx.createHttpServer(serverOptions).requestHandler(httpsRouter::accept).listen(serverOptions.getHttpsPort(), res -> {
                    if (res.succeeded()) {
                        System.out.println(appOption.getAppName() + " Running on port " + serverOptions.getHttpsPort() + " by HTTPS");
                        createHttps.handle(Future.succeededFuture());
                    } else {
                        System.out.println("create HTTPS Server failed : " + res.cause());
                        createHttps.handle(Future.failedFuture(res.cause()));
                    }
                });
            } else {
                LOG.error("执行创建https服务器-->失败:无效的证书或者错误的路径:如果证书存放在conf/cert中,路径可以从cert/开始,示例:cert/XXX.XXX");
                createHttps.handle(Future.failedFuture("无效的证书或者错误的路径"));
            }
        } else {
            LOG.error("执行创建https服务器-->失败:无效的证书或者错误的路径:如果证书存放在conf/cert中,路径可以从cert/开始,示例:cert/XXX.XXX", check.cause());
            createHttps.handle(Future.failedFuture(check.cause()));
        }
    });
}
Also used : ClusteredSessionStore(io.vertx.ext.web.sstore.ClusteredSessionStore) SessionStore(io.vertx.ext.web.sstore.SessionStore) LocalSessionStore(io.vertx.ext.web.sstore.LocalSessionStore) SessionHandler(io.vertx.ext.web.handler.SessionHandler) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpServerResponse(io.vertx.core.http.HttpServerResponse) CorsHandler(io.vertx.ext.web.handler.CorsHandler) VxApiCertOptions(com.szmirren.vxApi.core.options.VxApiCertOptions) PfxOptions(io.vertx.core.net.PfxOptions)

Example 8 with PemKeyCertOptions

use of io.vertx.core.net.PemKeyCertOptions in project vertx-openshift-it by cescoffier.

the class HelloHttp2Verticle method start.

@Override
public void start() throws Exception {
    vertx.exceptionHandler(Throwable::printStackTrace);
    HttpServer server = vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("server-key.pem").setCertPath("server-cert.pem")));
    server.requestHandler(req -> {
        System.out.println("Handling request on Aloha " + req.version());
        req.response().putHeader("content-type", "text/html").end("<html><body>" + "<h1>Aloha from vert.x!</h1>" + "<p>version = " + req.version() + "</p>" + "</body></html>");
    }).listen(8081);
}
Also used : PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions)

Example 9 with PemKeyCertOptions

use of io.vertx.core.net.PemKeyCertOptions in project vert.x by eclipse.

the class KeyStoreTest method testKeyCertValue.

@Test
public void testKeyCertValue() throws Exception {
    PemKeyCertOptions options = Cert.SERVER_PEM.get();
    Buffer key = vertx.fileSystem().readFileBlocking(options.getKeyPath());
    options.setKeyValue(null).setKeyValue(key);
    Buffer cert = vertx.fileSystem().readFileBlocking(options.getCertPath());
    options.setCertValue(null).setCertValue(cert);
    testKeyStore(options);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) Test(org.junit.Test)

Example 10 with PemKeyCertOptions

use of io.vertx.core.net.PemKeyCertOptions in project vert.x by eclipse.

the class KeyStoreTest method testKeyCertOptions.

@Test
public void testKeyCertOptions() throws Exception {
    PemKeyCertOptions options = new PemKeyCertOptions();
    assertNull(options.getKeyPath());
    String randString = TestUtils.randomAlphaString(100);
    assertEquals(options, options.setKeyPath(randString));
    assertEquals(randString, options.getKeyPath());
    assertNull(options.getCertPath());
    randString = TestUtils.randomAlphaString(100);
    assertEquals(options, options.setCertPath(randString));
    assertEquals(randString, options.getCertPath());
}
Also used : PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) Test(org.junit.Test)

Aggregations

PemKeyCertOptions (io.vertx.core.net.PemKeyCertOptions)22 Test (org.junit.Test)10 HttpServerOptions (io.vertx.core.http.HttpServerOptions)9 HttpServer (io.vertx.core.http.HttpServer)7 AbstractVerticle (io.vertx.core.AbstractVerticle)4 Buffer (io.vertx.core.buffer.Buffer)4 PfxOptions (io.vertx.core.net.PfxOptions)4 Router (io.vertx.ext.web.Router)4 HttpServerResponse (io.vertx.core.http.HttpServerResponse)3 JsonObject (io.vertx.core.json.JsonObject)3 JksOptions (io.vertx.core.net.JksOptions)3 HttpClient (io.vertx.core.http.HttpClient)2 HttpClientOptions (io.vertx.core.http.HttpClientOptions)2 HttpClientRequest (io.vertx.core.http.HttpClientRequest)2 HttpMethod (io.vertx.core.http.HttpMethod)2 HttpVersion (io.vertx.core.http.HttpVersion)2 JsonArray (io.vertx.core.json.JsonArray)2 OpenSSLEngineOptions (io.vertx.core.net.OpenSSLEngineOptions)2 PemTrustOptions (io.vertx.core.net.PemTrustOptions)2 KeyStoreHelper (io.vertx.core.net.impl.KeyStoreHelper)2