gpt4 book ai didi

java - 我如何以编程方式为 Spring Boot 和 Tomcat 提供 keystore 文件?

转载 作者:行者123 更新时间:2023-12-04 03:59:38 25 4
gpt4 key购买 nike

我知道我们可以使用配置 keystore 的文件位置

server.ssl.key-store=file:/path/to/file.p12

出于安全考虑,我们希望删除磁盘上的 P12 文件并直接从云提供商库中获取它。由于可以配置 keystore 的密码,我可以使用 https://stackoverflow.com/a/44971126/4460877 进行设置。

是否有类似的方法来配置 keystore 文件而不是通过从云提供商获取文件位置来配置它?

最佳答案

我能够使用 WebServerFactoryCustomizer 以编程方式设置 keystore 文件,如下所示

    @Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatSslStoreCustomizer() {
// Supply key store password
String keyStorePassword;
// Supply key store file as a stream
InputStream keyStoreFile;
KeyStore keyStore;

try (InputStream is = keyStoreFile) {
keyStore = KeyStore.getInstance(KEY_STORE_TYPE);
keyStore.load(is, keyStorePassword.toCharArray());
}
catch (Exception e) {
throw new RuntimeException("Cannot load keystore file; cause: " + e.getMessage(), e);
}

return tomcat -> tomcat.setSslStoreProvider(new SslStoreProvider() {
@Override
public KeyStore getKeyStore() {
return keyStore;
}

@Override
public KeyStore getTrustStore() {
return null;
}
});
}

关于java - 我如何以编程方式为 Spring Boot 和 Tomcat 提供 keystore 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63256949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com