Spring Boot Application Secured by Let’s Encrypt Certificate

In this article, we become familiar how to generate a standalone certificate using Let’s Encrypt authority and how to make it work with Spring Boot application. Let’s Encrypt made it easy and free for the whole web to obtain a valid certificate.

Introduction

In previous article, we became familiar with configuration of an Spring Boot Application with a self-signed certificate. Self-signed certificates are good for specific purposes such as test and development. But, if one needs to ship his application to production, certificates should be signed by known and eligible Certificate Authorities (CA).

These types of certificates are usually expensive. If you want to harden your application with TLS, you need to purchase one of them. The price and complex configuration of application servers made a barrier for many web applications to use secure connections.

In the post-Snowden era, no one needs to convince us that having secure connection using HTTPS is a must. There are lots of efforts to increase awareness of developers and IT administrators to employ such technologies for every single website they make. But how?

Let’s Encrypt projects aims at bringing HTTPS to World Wide Web not only for free but also with the simplest way of configuration.

How to Generate Certificates with Let’s Encrypt

Let’s Encrypt has several plugins for some application servers such as Apache and Nginx. In this section, as our target is Spring Boot Application (with an embedded Jetty/Tomcat), we just generate the certificates and later on integrate with our application.

If you’re using a firewall or any other security mechanism at your server or Cloud provider, you should relax it for a couple of minutes – specially port 80 and port 443.

Port 80 should be open and free to use as Let’s Encrypt runs a small http server behind the scene to prove whether you control your domain address (ACME protocol).

  1. You need to fetch the source code of Let’s Encrypt on your server which your domain address is pointing to. This step may take a couple minutes.

    Remark: Python 2.7.8 (or above) should be installed beforehand.
  2. By executing following command in your terminal, Let’s Encrypt generates certificates and a private key for you.

    Keys are generated in /etc/letsencrypt/live/example.com.
    Remark:
    ‘certonly’ – means that this command does not come with any special plugin like Apache or Nginx.
    ‘standalone’ –  means that Let’s encrypt will automatically create a simple web server on port 80 to prove you control the domain.

How to Generate PKCS12 files from PEM files

Certificates and private keys are generated in 2 steps for free which shows the simplicity of Let’s Encrypt. All of these generated materials are with PEM extension which is not supported in Spring Boot.

Spring-Boot does not support PEM files generated by Let’s Encrypt. Spring Boot supports PKCS12 extension. Using OpenSSL, we convert our certificate and private key to PKCS12.

To convert the PEM files to PKCS12 version:

  1. Go to /etc/letsencrypt/live/example.com
  2. We convert the keys to PKCS12 using OpenSSL in the terminal as follows.

The file ‘keystore.p12’ with PKCS12 is now generated in ‘/etc/letsencrypt/live/example.com’.

Configuration of your Spring Boot Application

Now we want to configure our Spring Boot application to benefit from the certificate and the private key; and eventually have the HTTPS thingy ready. At this moment, we already generated our certificate and private key. Then we converted the keys to PKCS12 extension which is ready to be used for Spring application.

  1. Open your ‘application.properties’
  2. Put this configuration there.

    Remark
    ‘requiressl’ – means that your server only processes HTTPS-protected requests.

If you visit https://example.com:8443, you can see that HTTPS is successfully configured and most importantly working.

Screen Shot 2016-05-14 at 09.47.32

As illustrated above, the server is secured by Let’s Encrypt certificate. For privacy reasons, I hid the domain name in this picture.

 

Further Information

Certificate renewal. Certificates issued by Let’s Encrypt are only valid for 90 days. They believe that by using short-lived keys, damage from probable key compromise and mis-issuance is limited. It means that every 60 days you should renew your certificates. Let’s Encrypt client applications are evolving to provide automatic renewal process.

Limit on usage. Let’s Encrypt issues a limited number of certificates per week. If you plan to explore and try the configurations, you can use the ‘–test-cert’ flag in your generation command. Then you receive certificate from their staging server.

Configuration quality. Having a strong and large key is not always sufficient. Your application server should also be configured correctly. SSL Labs provides a very useful online tool to evaluate your HTTPS configuration. In addition to SSL Labs, there is a list of assessment tool in this link.

ACME Community clients. There are (evolving) list of community clients of ACME protocol. These client apps or scripts aim at assisting developers or IT administrators in order to automate the whole process with different languages and environments.

Future Work

Considering the fact that Spring applications are one of the most popular application stacks in enterprise Java world, having a module responsible for the whole process is a must. I am not sure Spring team have tendency to add a Let’s Encrypt plugin to their well-established Spring Security module.

But there are a couple of community projects trying to bring ACME protocol to Java-based projects such as Acme-Client and Acme4j. We need an extra community move towards Spring framework to benefit from a neat module integrated with Spring framewok automating everything by 2 lines of configurations. If I find time, I may give it a try to implement such module for Spring.

Ethically we should support projects like Let’s Encrypt which are clearly for good. Our basic support is to spread the news and perhaps contributing as much as we can.

Facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

6 thoughts on “Spring Boot Application Secured by Let’s Encrypt Certificate

  1. Thank you so much for this blog post. It helped me out a lot. Just did setup the backend server with a Let’s Encrypt certificate. I did replace my old GoDaddy one which was about the expire very soon.
    In the beginning I was kinda scared, as setting up the GoDaddy one was a total mess.
    Thanks to Let’s Encrypt getting a SSL certificate, setting it up and renewing it is like the easiest thing in the world <3

  2. I’m always getting this error:
    java.lang.IllegalArgumentException: java.io.IOException: DerInputStream.getLength(): lengthTag=109, too big.

    When I try to deploy it to AWS.
    Do you know what could be the problem?
    Thanks

  3. I’m getting the below error message in chrome when following this tutorial… Any clue?

    This site can’t provide a secure connection
    my.domain.com uses an unsupported protocol.

    ERR_SSL_VERSION_OR_CIPHER_MISMATCH

    • And in firefox I’m getting:

      An error occurred during a connection to my.domain.com. Cannot communicate securely with peer: no common encryption algorithm(s). Error code: SSL_ERROR_NO_CYPHER_OVERLAP

Leave a Reply

Your email address will not be published. Required fields are marked *