简体   繁体   English

添加 ssl 证书 spring 启动应用程序无法启动后

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

After i added ssl certificate by the instruction on this sitehttps://www.thomasvitale.com/https-spring-boot-ssl-certificate/ application fails to start with following error:在我按照本网站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. 

When i change the port i get the same error.当我更改端口时,我得到同样的错误。 netstat -ntulp shows there is no programms are running on that port. netstat -ntulp显示该端口上没有正在运行的程序。 When i comment lines associated with ssl in application.properties the server starts normally.当我在 application.properties 中注释与 ssl 关联的行时,服务器正常启动。 Please help me请帮我

Try to configure your backend just like that:尝试像这样配置您的后端:

  1. Add dependency:添加依赖:

     implementation 'org.apache.httpcomponents:httpclient:4.5'
  2. Provide RestTemplate bean:提供 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. Provide required SSL properties in your application.properties or application.yaml file:在 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

That's it.而已。 Now you can see your Tomcat is starting on 8080 (or another port) (https).现在您可以看到您的 Tomcat 从 8080(或其他端口)(https)开始。

Alternatively, you can use my spring boot starter或者,您可以使用我的 spring 启动器

Look further down your stack trace.进一步查看您的堆栈跟踪。 You'll most probably see a section in your stack trace related to some SSL exception, preventing Tomcat from starting up correctly.您很可能会在堆栈跟踪中看到与某些 SSL 异常相关的部分,从而阻止 Tomcat 正确启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM