Spring Boot SSL:如何信任所有有效证书

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

是否可以自动信任我的证书,而无需手动将其添加到信任库中?

在启用了 SSLSpring Boot 中,我想使用 REST 服务,例如 Google APIsFacebook。 我必须在Truststore中添加证书。 OpenSSL 获取证书。 Keytool 导入到 Truststore

> openssl s_client -connect googleapis.com:443 
> keytool.exe -import -noprompt -trustcacerts  -alias googleapis.com -file googleapis.com.cer -keystore app-server.p12 -storepass *****

问题是管理起来很不方便,当证书过期时我每次都必须更新信任库中的证书。 即使我添加了正确的证书,我有时也会遇到此错误。 我需要一个信任库,因为我使用使用 Keytool 生成的自签名证书。如果没有,我的服务就无法相互通信。

错误
Caused by: javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to
find valid certification path to requested target
SSL 配置
ssl:
    enabled: true
    key-store: classpath:keystore/app-server.p12
    key-store-password: ******
    key-alias: app-server
    key-store-type: PKCS12
    trust-store: classpath:keystore/app-server.p12
    trust-store-password: *****
    trust-store-type: PKCS12
    keyStorePath: config/keystore/app-server.p12
主类片段
private static String keyStorePath;
private static String keyStorePassword;

@Value("${server.ssl.keyStorePath}")
public void setKeyStorePath(String keyStorePath) {
    ClientUiApplication.keyStorePath = keyStorePath;
}

@Value("${server.ssl.key-store-password}")
public void setKeyStorePassword(String keyStorePassword) {
    ClientUiApplication.keyStorePassword = keyStorePassword;
}

public static void main(String[] args) {
    SpringApplication.run(ClientUiApplication.class, args);
    System.setProperty("javax.net.ssl.trustStore", keyStorePath);
    System.setProperty("javax.net.ssl.trustStorePassword", keyStorePassword);
}
openssl ssl-certificate keystore keytool spring-annotations
2个回答
4
投票

您提供代码:

public static void main(String[] args) {
    SpringApplication.run(ClientUiApplication.class, args);
    System.setProperty("javax.net.ssl.trustStore", keyStorePath);
    System.setProperty("javax.net.ssl.trustStorePassword", keyStorePassword);
}

这意味着您强制 java 信任存储成为您提供的信任存储。默认信任存储不再使用。

所以,是的,您必须在此信任存储中添加所有需要的根证书,才能避免出现您所描述的问题。

为什么需要有特定的信任存储?

  • 如果没有用,请将其删除。
  • 如果您有必须信任的特定附加证书,最好将此证书添加到默认信任存储(
    jre/lib/security/cacerts
    文件)

2
投票

您还可以使用

CloseableHttpClient

绕过 SSL 证书检查
public static CloseableHttpClient getCloseableHttpClient()
{
    CloseableHttpClient httpClient = null;
    try {
        httpClient = HttpClients.custom().
                setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).
                setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy()
                {
                    public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException
                    {
                        return true;
                    }
                }).build()).build();
    } catch (KeyManagementException e) {
        LOGGER.error("KeyManagementException in creating http client instance", e);
    } catch (NoSuchAlgorithmException e) {
        LOGGER.error("NoSuchAlgorithmException in creating http client instance", e);
    } catch (KeyStoreException e) {
        LOGGER.error("KeyStoreException in creating http client instance", e);
    }
    return httpClient;
}

它将自动信任所有证书并让您免于

SSLHandshakeException: sun.security.validator.ValidatorException
。 请注意,在生产中不建议使用此方法。

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