【问题标题】:Spring Boot - HTTPS on localhostSpring Boot - 本地主机上的 HTTPS
【发布时间】:2019-03-12 13:09:43
【问题描述】:

我在 Spring Boot 中创建了一个应用程序,并使用以下配置在 application.properties 中启用了 SSL

server.port=8085
server.ssl.key-store=classpath:keystore.jks
server.ssl.client-auth=need
server.ssl.key-alias=selfsigned
server.ssl.key-store-password=password
server.ssl.key-password=password

我还创建了自签名证书 keystore.jks 用于通过 localhost 访问。

下面是keytool -list -keystore keystore.jks -v的结果

Keystore type: jks
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: selfsigned
Creation date: 5-okt-2018
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=localhost, OU=UniteInboxAPI, OU=DEV, OU=PKI, OU=Services, O=ING, L=Holualoa, ST=HI, C=US
Issuer: CN=localhost, OU=UniteInboxAPI, OU=DEV, OU=PKI, OU=Services, O=ING, L=Holualoa, ST=HI, C=US
Serial number: 68547095
Valid from: Fri Oct 05 17:24:46 CEST 2018 until: Sat Oct 05 17:24:46 CEST 2019
Certificate fingerprints:
         MD5:  E5:48:B0:2F:DA:5C:BE:8E:30:A9:A6:CF:B3:07:55:DC
         SHA1: EC:C2:B2:F5:70:CA:57:47:8F:54:A7:5E:54:C2:A1:29:51:2F:51:62
         SHA256: 7F:EA:88:65:24:A7:39:20:93:14:54:0D:53:B7:50:85:D9:8B:55:5F:72:43:EB:94:99:FC:93:CE:25:4A:BA:27
Signature algorithm name: SHA256withRSA
Subject Public Key Algorithm: 2048-bit RSA key
Version: 3

当我尝试通过 Chrome/Mozilla 访问它时,主机无法访问,并且我的端点未提供服务。

如果我需要做任何额外的配置/必须在浏览器中导入任何证书,请提供帮助。

问候, 苏沃吉特

【问题讨论】:

  • 多年来我从来不需要在本地运行 SSL,您确定这是您真正需要的吗?一种常见的部署模式是,您的非 HTTPS 应用程序在负载均衡器后面的对等集群中运行,这通常是 SSL 终止发生的地方。我知道可能有无数其他的部署模式,我当然已经看过并做过其中的一些,只是从来没有需要在本地搞乱 HTTPS...
  • 是的,为了实现的目的,测试证书和相互身份验证需要这样做:)
  • (1) 是的,浏览器(或任何体面的客户端)要信任自签名证书,您需要将该证书添加到其信任库。使用keytool -export -keystore whatever [-alias whatever] [-rfc] -file outfile 获取文件中的证书。 Firefox 使用自己的商店,您必须从其选项/高级/证书选项卡中添加。 Chrome 使用平台的商店,可以通过设置+高级/管理证书或直接访问。这两者都应该处理 DER 或 PEM (-rfc) 格式,尽管如果 以后想查看或使用该文件,后者更方便。 ...
  • ... (2) 但是,这应该给出一个关于“不可信”或“非私有”的错误——不是“不可访问”,这表明你还有其他错误或问题.但如果没有细节,实际上是不可能提供帮助的。错误消息到底是什么?您是否尝试过像curlwget 甚至openssl s_client 这样的非浏览器客户端,如果有错误,您会遇到什么错误? (openssl s_client 接受自签名或其他不可信的证书。)
  • 我在 Mozilla (SSL_ERROR_BAD_CERT_ALERT) 中收到此错误,在 chrome 中它给了我 401 Unauthorized 错误。有什么方法可以为我的本地主机禁用 SSL?

标签: java spring-boot


【解决方案1】:

尝试像这样配置您的 RestTemplate:

  1. 添加依赖:

     implementation 'org.apache.httpcomponents:httpclient:4.5'    
    
  2. 提供 RestTemplate bean:

@Bean
private RestTemplate restTemplate() {
        SSLContext sslContext = buildSslContext();
        SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);

        HttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(socketFactory)
                .build();

        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);

        return new RestTemplate(factory);
    }

private SSLContext buildSslContext() {
        try {
            char[] keyStorePassword = sslProperties.getKeyStorePassword();
            return new SSLContextBuilder()
                    .loadKeyMaterial(
                            KeyStore.getInstance(new File(sslProperties.getKeyStore()), keyStorePassword),
                            keyStorePassword
                    ).build();
        } catch (Exception ex) {
            throw new IllegalStateException("Unable to instantiate SSL context", ex);
        } finally {
            sslProperties.setKeyStorePassword(null);
            sslProperties.setTrustStorePassword(null);
        }
    }
  1. 在您的 application.properties 或 application.yaml 文件中提供所需的 SSL 属性:
server:
    ssl:
        enabled: true
        key-store: /path/to/key.keystore
        key-store-password: password
        key-alias: alias
        trust-store: /path/to/truststore
        trust-store-password: password

就是这样。现在您可以看到您的 Tomcat 在 8080(或其他端口)(https) 上启动。

或者,您可以使用my spring boot starter

【讨论】:

    猜你喜欢
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 2019-02-05
    • 2020-04-03
    相关资源
    最近更新 更多