Configure A Spring Boot Application For Https

1 minute read

Generate a self-signed certificate using Keytool

Use the following command to generate a self-signed RSA certificate that can be used during development to test your application under HTTPS.

keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 7300

For production, you will want to create a certificate via a Certificate Authority. Read How to convert a web application from HTTP to HTTPS for more information on how to create a production-ready HTTPS certificate.

Modify your application.properties file

To configure Spring Boot for HTTPS, all you need to do is add the following properties to the application.properties file in /src/main/resources/application.properties.

server.port: 443
server.ssl.key-store: keystore.p12
server.ssl.key-store-password: password
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: tomcat

Now, when you run your application make sure that the “keystore.p12” file that you generated is in the same directory as your application, or you must specify the path to it in your application.properties. Note: it must be in the same directory as your deployed application (jar), not in the same directory as the application.properties file.

You will now be able to access your application from https://localhost.

Redirect HTTP to HTTPS (optional)

It may be necessary to redirect an existing HTTP request to use the new HTTPS protocol, especially if you are changing an existing application. Read How to convert a web application from HTTP to HTTPS for more information on how to do this.

Updated: