簡體   English   中英

添加 ssl 證書 spring 啟動應用程序無法啟動后

[英]After adding ssl certificate spring boot application fails to start

在我按照本網站https://www.thomasvitale.com/https-spring-boot-ssl-certificate/的說明添加 ssl 證書后,應用程序無法啟動並出現以下錯誤:

***************************                                                                               
APPLICATION FAILED TO START                                                                               
***************************                                                                               

Description:                                                                                              

The Tomcat connector configured to listen on port 80 failed to start. The port may already be in use or th
e connector may be misconfigured. 

當我更改端口時,我得到同樣的錯誤。 netstat -ntulp顯示該端口上沒有正在運行的程序。 當我在 application.properties 中注釋與 ssl 關聯的行時,服務器正常啟動。 請幫我

嘗試像這樣配置您的后端:

  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)開始。

或者,您可以使用我的 spring 啟動器

進一步查看您的堆棧跟蹤。 您很可能會在堆棧跟蹤中看到與某些 SSL 異常相關的部分,從而阻止 Tomcat 正確啟動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM