ssl - 以编程方式为 Jetty 9 嵌入式配置 SSL

标签 ssl jetty

我正在使用 9.0.0.M4 版本的 Jetty,并尝试将其配置为接受 SSL 连接。 按照以下说明进行操作: http://www.eclipse.org/jetty/documentation/current/configuring-connectors.html

我已经设法写出一些有用的东西。 然而,我写的代码看起来很丑陋而且不必要地复杂。 知道如何正确执行此操作吗?

final Server server = new Server(Config.Server.PORT);

SslContextFactory contextFactory = new SslContextFactory();
contextFactory.setKeyStorePath(Config.Location.KEYSTORE_LOCATION);
contextFactory.setKeyStorePassword("******");
SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(contextFactory, org.eclipse.jetty.http.HttpVersion.HTTP_1_1.toString());

HttpConfiguration config = new HttpConfiguration();
config.setSecureScheme("https");
config.setSecurePort(Config.Server.SSL_PORT);
config.setOutputBufferSize(32786);
config.setRequestHeaderSize(8192);
config.setResponseHeaderSize(8192);
HttpConfiguration sslConfiguration = new HttpConfiguration(config);
sslConfiguration.addCustomizer(new SecureRequestCustomizer());
HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(sslConfiguration);

ServerConnector connector = new ServerConnector(server, sslConnectionFactory, httpConnectionFactory);
connector.setPort(Config.Server.SSL_PORT);
server.addConnector(connector);

server.start();
server.join();

最佳答案

ServerConnector应该设置一个 SslContextFactory .

您在 HttpConfiguration 中完成的其余工作与设置 SSL 无关。

embedded jetty examples 中维护了一个在嵌入式模式下设置 SSL 的好例子。项目。 http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java

编辑:更清楚(感谢 Erik)

更新:2016 年 6 月

Eclipse Jetty 项目已将其规范存储库移至 github。

上面的LikeJettyXml.java现在可以在

https://github.com/eclipse/jetty.project/blob/jetty-9.4.x/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java

关于ssl - 以编程方式为 Jetty 9 嵌入式配置 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14362245/

相关文章:

ssl - HTTPS 连接被拒绝,使用 ingress-nginx

java - Jetty 中的这个配置有什么问题吗?无法运行任何上下文

elasticsearch - 使用NEST,在JetSearch中通过身份验证的ElasticSearch中建立索引文档

java - 使用嵌入式 Jetty 在根上下文中提供 index.html

php - 给定特定 url 参数时强制使用非 SSL

java - 自签名证书与 CA

iOS 到 Node.js 后端 SSL 握手失败

Android 6 无法使用自签名证书与安全服务器通信

java - 一个进程中的两个 Jetty 嵌入式服务器实例

memory-leaks - Jetty 8 服务器上的内存泄漏