How can I set up a letsencrypt SSL certificate and use it in a Spring Boot application?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To use a Let's Encrypt certificate in a Spring Boot application, you need two separate pieces to work together: certificate issuance and TLS configuration. The certificate is usually obtained with Certbot, then converted into a Java-friendly keystore so Spring Boot can serve HTTPS directly.
Issue the Certificate with Certbot
Let's Encrypt validates that you control the domain before issuing a certificate. The easiest flow is HTTP validation on port 80, so the server must be reachable from the public internet and the DNS record for your domain must already point to the machine.
On a Linux server, install Certbot and request the certificate:
After success, Certbot writes the certificate files under:
The important files are:
- '
fullchain.pemfor the server certificate plus intermediate chain' - '
privkey.pemfor the private key'
Spring Boot does not normally read those PEM files directly in older setups, so a common approach is to convert them into a PKCS#12 keystore.
Convert PEM Files into a PKCS#12 Keystore
Java SSL configuration is most straightforward when you provide a .p12 file. openssl can combine the certificate and key into one keystore:
Choose a stronger password than changeit in real deployments. The application process must also have permission to read the generated keystore file.
If you prefer not to place private material under the app directory, keep it in a restricted location and point Spring Boot at that path instead.
Configure Spring Boot for HTTPS
Once the keystore exists, enable SSL in application.yml:
If you start the application now, it will listen on 8443 and serve HTTPS using the certificate from Let's Encrypt.
For a quick redirect from HTTP to HTTPS inside the app, you can add a second connector in code:
That lets clients hit 8080 and get redirected to the secure port.
Handle Renewal
Let's Encrypt certificates expire frequently, so renewal must be automatic. Certbot already knows how to renew, but your app also needs an updated keystore after renewal.
One practical solution is a deploy hook that rebuilds the .p12 file and restarts the service:
Then call it from Certbot:
Without this step, the renewed certificate exists on disk but Spring Boot keeps serving the old keystore.
Direct TLS vs Reverse Proxy
Serving HTTPS directly from Spring Boot works, but many production systems terminate TLS at Nginx, Apache, or a cloud load balancer. That design often simplifies renewal, HTTP to HTTPS redirects, compression, static asset caching, and certificate rotation.
If you only need secure transport quickly and run a single service, direct SSL in Spring Boot is acceptable. If you run multiple services or expect traffic growth, a reverse proxy is usually easier to operate.
Common Pitfalls
- Requesting a certificate before DNS points to the server. Let's Encrypt cannot validate a domain that does not resolve correctly.
- Forgetting that Spring Boot needs a keystore workflow, not just raw PEM files in older configurations.
- Renewing the Let's Encrypt certificate without rebuilding the
.p12file. The app continues serving the stale certificate. - Locking down file permissions too loosely.
privkey.pemand the generated keystore should be readable only by the correct service account. - Using port
443in production without confirming firewall and system service configuration. The app may be correct while the network still blocks HTTPS.
Summary
- Use Certbot to issue a Let's Encrypt certificate for your domain.
- Convert
fullchain.pemandprivkey.peminto a PKCS#12 keystore for Spring Boot. - Enable
server.sslin Spring Boot and point it at the keystore. - Automate renewal by rebuilding the keystore and restarting the service.
- Consider a reverse proxy if you want easier certificate management at scale.

