我正在使用springBootVersion 1.2.0.RELEASE。我正在尝试通过application.properties配置我的密钥库和信任库。
当我添加以下设置时,我可以让密钥库工作,但不能让信任库工作。
server.ssl.key-store=classpath:foo.jks
server.ssl.key-store-password=password
server.ssl.key-password=password
server.ssl.trust-store=classpath:foo.jks
server.ssl.trust-store-password=password但是,如果我通过gradle添加信任库:
bootRun {
jvmArgs = [ "-Djavax.net.ssl.trustStore=c://foo.jks", "-Djavax.net.ssl.trustStorePassword=password"]
}它工作得很好。
有没有人将application.properties用于信任商店?
发布于 2017-04-05 23:28:25
如果您需要进行REST调用,可以使用下一种方法。
这将适用于通过 RestTemplate.的去电
像这样声明RestTemplate bean。
@Configuration
public class SslConfiguration {
@Value("${http.client.ssl.trust-store}")
private Resource keyStore;
@Value("${http.client.ssl.trust-store-password}")
private String keyStorePassword;
@Bean
RestTemplate restTemplate() throws Exception {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(
keyStore.getURL(),
keyStorePassword.toCharArray()
).build();
SSLConnectionSocketFactory socketFactory =
new SSLConnectionSocketFactory(sslContext);
HttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(socketFactory).build();
HttpComponentsClientHttpRequestFactory factory =
new HttpComponentsClientHttpRequestFactory(httpClient);
return new RestTemplate(factory);
}
}其中http.client.ssl.trust-store和http.client.ssl.trust-store-password指向JKS格式的信任库和指定信任库的密码。
这将覆盖Spring Boot提供的RestTemplate bean,并使其使用您需要的信任存储。
发布于 2017-06-30 23:10:38
我在Spring Boot、Spring Cloud (微服务)和自签名SSL证书上也遇到了同样的问题。密钥库在应用程序属性中开箱即用,而信任库则没有。
最后,我将密钥库和信任库配置都保存在application.properties中,并添加了一个单独的配置bean,用于在系统中配置信任库属性。
@Configuration
public class SSLConfig {
@Autowired
private Environment env;
@PostConstruct
private void configureSSL() {
//set to TLSv1.1 or TLSv1.2
System.setProperty("https.protocols", "TLSv1.1");
//load the 'javax.net.ssl.trustStore' and
//'javax.net.ssl.trustStorePassword' from application.properties
System.setProperty("javax.net.ssl.trustStore", env.getProperty("server.ssl.trust-store"));
System.setProperty("javax.net.ssl.trustStorePassword",env.getProperty("server.ssl.trust-store-password"));
}
}发布于 2015-05-13 02:55:08
我也有同样的问题,我会试着更详细地解释一下。
我正在使用spring-boot 1.2.2-RELEASE,并在Tomcat和Undertow上试用,结果相同。
在application.yml中定义信任存储区,如:
server:
ssl:
trust-store: path-to-truststore...
trust-store-password: my-secret-password...不工作,而:
$ java -Djavax.net.debug=ssl -Djavax.net.ssl.trustStore=path-to-truststore... -Djavax.net.ssl.trustStorePassword=my-secret-password... -jar build/libs/*.jar 工作得很好。
查看rutime差异的最简单方法是在客户端启用ssl-debug。在工作时(即使用-D标志),会将类似以下内容写入控制台(在处理第一个请求期间):
trustStore is: path-to-truststore...
trustStore type is : jks
trustStore provider is :
init truststore
adding as trusted cert:
Subject: C=..., ST=..., O=..., OU=..., CN=...
Issuer: C=..., ST=..., O=..., OU=..., CN=...
Algorithm: RSA; Serial number: 0x4d2
Valid from Wed Oct 16 17:58:35 CEST 2013 until Tue Oct 11 17:58:35 CEST 2033如果没有-D标志,我会得到:
trustStore is: /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/security/cacerts
trustStore type is : jks
trustStore provider is :
init truststore
adding as trusted cert: ... (one for each CA-cert in the defult truststore)...and当执行一个请求时,我得到一个异常:
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target希望它能帮助我们更好地理解这个问题!
https://stackoverflow.com/questions/27724544
复制相似问题