web-dev-qa-db-fra.com

Activer HTTPS avec un certificat auto-signé dans Spring Boot 2.0

Je suis ce tutoriel pour activer HTTPS dans Spring Boot 2.0 à l'aide d'un certificat auto-signé, uniquement à des fins de test. En résumé, ce tutoriel comprend les étapes suivantes:

1.Générez le magasin de clés à l'aide de keytool.

keytool -genkey -alias Tomcat
 -storetype PKCS12 -keyalg RSA -keysize 2048
 -keystore keystore.p12 -validity 3650

2.Activez HTTPS dans Spring Boot en ajoutant des propriétés dans le fichier application.properties.

server.port: 8443
server.ssl.key-store: keystore.p12
server.ssl.key-store-password: mypassword
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: Tomcat

HTTP HTTP 3.Redirect à HTTPS (facultatif). J'ai ignoré cette partie.

Mais quand je commence mon application, j'ai eu cette erreur:

org.Apache.catalina.LifecycleException: Failed to start component [Connector[HTTP/1.1-8443]]
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:167) ~[Tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.Apache.catalina.core.StandardService.addConnector(StandardService.Java:225) ~[Tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.springframework.boot.web.embedded.Tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.Java:255) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.web.embedded.Tomcat.TomcatWebServer.start(TomcatWebServer.Java:197) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.Java:300) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.Java:162) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:552) [spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.Java:140) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.Java:752) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.Java:388) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.Java:327) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.Java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.Java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at epic.gwdg.restgraph.RestgraphApplication.main(RestgraphApplication.Java:10) [classes/:na]
Caused by: org.Apache.catalina.LifecycleException: Protocol handler start failed
    at org.Apache.catalina.connector.Connector.startInternal(Connector.Java:1021) ~[Tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150) ~[Tomcat-embed-core-8.5.28.jar:8.5.28]
    ... 13 common frames omitted
Caused by: Java.lang.IllegalArgumentException: Private key must be accompanied by certificate chain
    at org.Apache.Tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.Java:116) ~[Tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.Apache.Tomcat.util.net.AbstractJsseEndpoint.initialiseSsl(AbstractJsseEndpoint.Java:87) ~[Tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.Apache.Tomcat.util.net.NioEndpoint.bind(NioEndpoint.Java:225) ~[Tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.Apache.Tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.Java:1150) ~[Tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.Apache.coyote.AbstractProtocol.start(AbstractProtocol.Java:591) ~[Tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.Apache.catalina.connector.Connector.startInternal(Connector.Java:1018) ~[Tomcat-embed-core-8.5.28.jar:8.5.28]
    ... 14 common frames omitted
Caused by: Java.lang.IllegalArgumentException: Private key must be accompanied by certificate chain
    at Java.base/Java.security.KeyStore.setKeyEntry(KeyStore.Java:1170) ~[na:na]
    at org.Apache.Tomcat.util.net.jsse.JSSEUtil.getKeyManagers(JSSEUtil.Java:257) ~[Tomcat-embed-core-8.5.28.jar:8.5.28]
    at org.Apache.Tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.Java:114) ~[Tomcat-embed-core-8.5.28.jar:8.5.28]
    ... 19 common frames omitted

2018-03-16 16:42:30.917  INFO 970 --- [           main] o.Apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-03-16 16:42:30.931  INFO 970 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-03-16 16:42:30.933 ERROR 970 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

The Tomcat connector configured to listen on port 8443 failed to start. The port may already be in use or the connector may be misconfigured.

Action:

Verify the connector's configuration, identify and stop any process that's listening on port 8443, or configure this application to listen on another port.

2018-03-16 16:42:30.934  INFO 970 --- [           main] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@58ce9668: startup date [Fri Mar 16 16:42:26 CET 2018]; root of context hierarchy
2018-03-16 16:42:30.936  INFO 970 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

Process finished with exit code 1

En gros, le message est le suivant: 

La clé privée doit être accompagnée d'une chaîne de certificats.

Comme il s'agit d'un certificat auto-signé, il n'a bien sûr pas la chaîne de confiance. Comment puis-je le réparer?

Voici mon fichier application.properties actuel:

server.port=8443
server.ssl.enabled=true
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-password=123456
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=Tomcat

Je vous remercie beaucoup pour votre aide.

12
Triet Doan

Le problème est que dans votre magasin de clés généré, vous n'avez pas de paire de clés. Il n'y a donc pas de clé privée car, en utilisant l'option -genkey, vous devez la changer avec l'option -genkeypair:

-genkey génère une clé secrète alors que -genkeypair génère un paire de clés (une clé publique et une clé privée).

Donc, je pense que cela devrait fonctionner: 

keytool -genkeypair -alias Tomcat -storetype PKCS12 -keyalg RSA -keysize 2048  -keystore keystore.p12 -validity 3650

Dans votre configuration de démarrage printanière, modifiez ":" par "=" et ajoutez le chemin d'accès à votre magasin de clés. Je suppose que votre magasin de clés.p12 se trouve dans votre dossier de ressources. 

server.ssl.key-store = classpath:keystore.p12
server.ssl.key-store-password = mypassword
server.ssl.key-store-type = PKCS12
server.ssl.key-alias = Tomcat
13
E2rabi

Je recevais aussi cette horrible erreur Private key must be accompanied by certificate chain sur mon application Spring Boot avec un serveur Tomcat intégré. Cela me rendait fou.

Il s'avère qu'une simple faute de frappe était mon problème:

@Override
public void customize(ConfigurableServletWebServerFactory server) {
    Ssl ssl = new Ssl();
    ssl.setEnabled(true);
    ssl.setKeyStore(keystoreFile);
    ssl.setKeyPassword(keystorePass); // << Should be `setKeyStorePassword` !!!!
    ssl.setKeyStoreType(keystoreType);
    ssl.setKeyAlias(keystoreAlias);

    server.setSsl(ssl);
    server.setPort(sslPort);
}

Donc, le message d'erreur n'est pas utile pas du tout pour ce cas. J'espère que ça aidera quelqu'un d'autre. Assurez-vous simplement que vous mettez les bons mots de passe (clé vs magasin de clés) au bon endroit. Le même problème peut se produire dans une configuration basée sur les propriétés - cela dépend de votre travail.

4
jocull

1. utiliser "-genkeypair"

keytool -genkeypair -alias Tomcat -storetype PKCS12 -keyalg RSA -keysize 2048  -keystore keystore.p12 -validity 3650
  1. remplacez "server.ssl.key-password" par "server.ssl. key-store-password "
1
Frank Liu

J'ai eu le même problème. J'ai fait les changements à partir de la 2e réponse. Mais le problème n’était pas parti… .. Après tout ce que j’ai fait, j’ai simplement inclus mon keystore.p12 certificate dans pom.xml dans la section profiles 

    <profiles>
    <!-- DEVELOPMENT PROFILE -->
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>application.properties</include>
                        <include>keystore.p12</include>
                        <include>data/**</include>
                    </includes>
                </resource>
            </resources>
        </build>
    </profile>
</profiles>
0
JenkaBY