嵌入式 Jetty 在没有证书的情况下启动,当证书可用时加载它

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

我在我的 Java 代码中嵌入了 Jetty 服务器。我正在使用

SSL
类加载
SslContextFactory
证书。我的问题是,当 Jetty 服务器启动时,
.jks
文件不存在于提到的文件路径中,因为它是动态生成并存储在路径中的。由于
.jks
文件不存在,因此服务器未启动并给出
NullPointerException
。当证书在路径上可用时,我将使用
sslContextFactory.reload()
,但我不确定如何在开始时处理
NullPointerException
。 有没有办法告诉 Jetty 在没有证书的情况下启动以及何时
sslContextFactory.reload()
重新加载证书。

更新

码头版本:9.4.26.v20200117

我使用下面的代码来加载证书。

 private final static String KEYSTORE_LOCATION = "/home/myproject/keystore.jks";

 SslContextFactory sslContextFactory = new SslContextFactory();
 sslContextFactory.setKeyStorePath(getKeystoreLocation());
 sslContextFactory.setKeyStorePassword("temp");
 sslContextFactory.setKeyManagerPassword("temp");

 private static String getKeystoreLocation() throws MalformedURLException {
    File file = new File(KEYSTORE_LOCATION);
    URL url = file.toURI().toURL();
    return url.toExternalForm();
}

我正在获取

NullPointerException
文件。你能告诉我这里缺少什么吗?

java-8 jetty embedded-jetty
2个回答
0
投票

使用 Jetty 9.4.46(或更高版本)。

Jetty 9.4.26 现在存在一些安全问题 - 请参阅 https://www.eclipse.org/jetty/security_reports.php 了解详细信息

这个

SslContextFactory
是供服务器使用还是供客户端使用?

服务器使用

SslContextFactory.Server
,客户端使用
SslContextFactory.Client
实现。 (您的旧版本 Jetty 可能没有这种区别,这是过去一些问题的根源)

启动 Jetty 后,配置的密钥库/信任库文件需要存在。

即使该密钥库/信任库文件是虚拟/空文件,它也需要存在。

一旦开始,您可以替换密钥库/信任库文件,SslContextFactory 将检测更改的文件并重新加载 SslContextFactory。


0
投票

这是我的 Jetty 服务器代码(Jetty 版本 - jetty-9.4.52.v20230823)。当我使用下面的代码时,SslContextFactory 重新加载不会更改密钥库。我做错了什么?密钥库类型为“PKCS12”。

更新: 尝试读取空密钥库时出现此错误:

java.io.IOException: DerInputStream.getLength(): lengthTag=127, too big.

        server = new Server();

        String keyStorePassword  = getKeyStorePassword();
        String keyStoreFile = getKeyStoreFile();
        String keyStoreType = getKeyStoreType(); // PKCS12

        SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
        sslContextFactory.setKeyStorePath(keyStoreFile);
        sslContextFactory.setKeyStoreType(keyStoreType);
        sslContextFactory.setKeyStorePassword(keyStorePassword);

        sslContextFactory.setExcludeCipherSuites(".*NULL.*", ".*RC4.*", ".*MD5.*", ".*DES.*", ".*DSS.*");
        sslContextFactory.setExcludeProtocols("SSLv3");
        sslContextFactory.setRenegotiationAllowed(false);

        // Reload when keystore file is changed
        sslContextFactory.reload(scf -> logger.info("Reloaded SSL cert"));

        HttpConfiguration httpConfiguration = new HttpConfiguration();
        // Suppress the Jetty server version from response header for security reasons
        httpConfiguration.setSendServerVersion(false);
        httpConfiguration.setSecureScheme("https");
        httpConfiguration.addCustomizer(new SecureRequestCustomizer());

        ServerConnector serverConnector = new ServerConnector(server,
                    new SslConnectionFactory(sslContextFactory, "http/1.1"),
                    new HttpConnectionFactory(httpConfiguration));
        serverConnector.setPort(httpsPort);
© www.soinside.com 2019 - 2024. All rights reserved.