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

问题描述 投票:0回答:4

我正在使用码头版本 9.0.0.M4 并尝试将其配置为接受 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();
ssl jetty
4个回答
12
投票

ServerConnector
应该设置
SslContextFactory
.

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

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

编辑:更清楚(感谢埃里克)

更新: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


5
投票

对于 Jetty 9,有一个很好的参考资料here,您需要做的就是按照 here 的说明创建 JKS 密钥库文件。 使用命令

keytool -genkey -alias sitename -keyalg RSA -keystore keystore.jks -keysize 2048
。出于某种原因,适用于码头 8 的不适用于码头 9。


0
投票

对于那些无法使上述配置工作的人: 如果您使用的是 java 1.7,请确保您有最新的更新。 jvm 1.7 的第一个版本导致访问 https 网页时出现问题(浏览器可能会显示:连接重置、连接中止或未收到数据错误)。


0
投票

对于 Eebbeded 码头服务器然后使用

SslContextFactory.Server sslContextFactory = new SslContextFactory.Server(); 这将解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.