All posts / Spring Boot

Securing Spring Boot APIs with Cacerts, KeyStore & TrustStore

NordLearn · · 4 min read

On this page

A production-grade guide to SSL/TLS security — why it matters, how the pieces fit together, and exactly how to configure it in Spring Boot.

Security in microservices isn’t just about who can call your API — it’s about trust at the wire level. When your Spring Boot service dials out to a database, an identity provider, or a payment gateway, how does it know the server on the other end is the real deal? The answer is SSL/TLS, and the key components are the KeyStore, the TrustStore, and the cacerts file.

In this post we’ll walk through every concept with visual flow diagrams, validate common misconceptions, and give you copy-paste-ready configuration for your Spring Boot application.

The Big Picture — Two Sides of SSL Identity

Before touching a single config file, let’s understand the two fundamental problems SSL/TLS solves:

Problem 1: Proving Your Identity

→ I am api.mycompany.com, and here’s my signed certificate to prove it.

→ Solved by the KeyStore.

→ Contains your private key + your server’s certificate.

Problem 2: Trusting Others

→ You claim to be payments.stripe.com — who vouches for you?

→ Solved by the TrustStore(backed by cacerts).

→ Contains CA certificates you’ve decided to trust

Diagram 1 — KeyStore vs TrustStore at a Glance

Diagram 1 — KeyStore vs TrustStore at a Glance

Why Does Your Server Need a KeyStore?

When a client (browser, another service, mobile app) connects to your Spring Boot API over HTTPS, it needs proof that it’s talking to the real you — not an impersonator. Your server proves its identity by presenting a digital certificate backed by a private key.

The KeyStore is a password-protected file that stores that private key plus your server’s certificate. Without it, your Spring Boot service simply cannot serve HTTPS traffic.

Why Does Your Server Need a TrustStore (Cacerts)?

Now your Spring Boot service is the client — it’s calling out to Stripe, an LDAP server, another microservice, or a cloud API. Before sending any data, Spring Boot needs to verify that the remote server’s certificate is legitimate. That’s what the TrustStore is for.

The Java runtime ships with a default TrustStore called cacerts located at $JAVA_HOME/lib/security/cacerts. It's pre-loaded with well-known public root Certificate Authorities (DigiCert, GlobalSign, etc.).

When cacerts isn’t enough

If your organization uses an internal CA (self-signed or corporate PKI), or you’re connecting to a service using a private certificate, that CA won’t be in the default cacerts file. You must add it manually — or every HTTPS call will throw an SSLHandshakeException.

Diagram 2— With vs Without Proper Cacerts (MITM Attack)

Diagram 2— With vs Without Proper Cacerts (MITM Attack)

Configuring HTTPS in Spring Boot (Embedded Tomcat)

Spring Boot ships with an embedded servlet container (Tomcat by default). You don’t need an external application server. SSL configuration is done entirely through application.properties or application.yml.

Step 1 — Generate a KeyStore with keytool

# Generate a self-signed certificate for development
keytool -genkeypair \
  -alias myspringapp \
  -keyalg RSA \
  -keysize 2048 \
  -storetype PKCS12 \
  -keystore keystore.p12 \
  -validity 3650 \
  -storepass changeit

Step 2 — application.properties (HTTPS Config)

# HTTPS Port
server.port=8443

# KeyStore (Your Identity)
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=changeit
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=myspringapp

# TrustStore (Who You Trust)
server.ssl.trust-store=classpath:truststore.p12
server.ssl.trust-store-password=changeit
server.ssl.trust-store-type=PKCS12

# Protocol + Cipher Suites 
server.ssl.protocol=TLS
server.ssl.enabled-protocols=TLSv1.2,TLSv1.3

Configuring TrustStore for Outbound HTTPS Calls

When your Spring Boot service calls external APIs, it needs to validate their certificates. Here are your options:

Option A — application.properties

server.ssl.trust-store=classpath:truststore.p12
server.ssl.trust-store-password=changeit
server.ssl.trust-store-type=PKCS12

Option B — JVM System Properties

java -jar myapp.jar \
  -Djavax.net.ssl.trustStore=/path/to/truststore.jks \
  -Djavax.net.ssl.trustStorePassword=changeit \
  -Djavax.net.ssl.trustStoreType=JKS

Understanding the cacerts File

The cacerts file is the default TrustStore bundled with every JDK/JRE. Its default password is changeit. It contains ~100+ trusted public CA root certificates.

# View all trusted CAs in your JDK's cacerts
keytool -list -keystore $JAVA_HOME/lib/security/cacerts \
  -storepass changeit

When You Need to Update cacerts

  • INTERNAL CA CERTIFICATES
    Your company uses an internal PKI. Services inside the network use certificates signed by a private CA that isn’t in the public trust chain.
  • SELF-SIGNED CERTIFICATES
    Development/staging environments often use self-signed certs. They’re not signed by any CA, so they fail validation by default.
  • NEW THIRD-PARTY CA
    A vendor switches to a CA that wasn’t trusted in older JDKs. Older Java versions may not have the newer root cert.
  • EXPIRED ROOT CERTIFICATES
    Root CAs expire (rarely, but it happens).

Adding a Certificate to cacerts

# Step 1: Export the remote server's certificate
openssl s_client -connect internal.mycompany.com:443 \
  -showcerts /dev/null \
  | openssl x509 -outform PEM > server_cert.crt

# Step 2: Import it into cacerts
keytool -import \
  -alias myinternalca \
  -file server_cert.crt \
  -keystore $JAVA_HOME/lib/security/cacerts \
  -storepass changeit \
  -noprompt

# Step 3: Verify it's there
keytool -list -alias myinternalca \
  -keystore $JAVA_HOME/lib/security/cacerts \
  -storepass changeit

Docker / Container Tip

If you’re running Spring Boot in Docker, modifying the JDK’s cacerts inside the image requires root. Better practice: copy your CA cert into the image and import during the Docker build step. Never mount the host’s cacerts directly into production containers.

Diagram 3— SSL Failure Decision Tree

Diagram 3— SSL Failure Decision Tree

Wrapping Up

SSL/TLS security in Spring Boot boils down to two files and two jobs:

The KeyStore holds your server’s identity — its private key and certificate. When clients connect to your API, you present this to prove who you are. The TrustStore (or cacerts) holds the list of Certificate Authorities you believe in. When your service makes outbound HTTPS calls, it checks the remote server's certificate against this list.

Get these right and you get: encrypted channels, verified identities, and a strong defense against man-in-the-middle attacks. Get them wrong and you get SSL handshake errors, production outages, and potential data breaches.

Spring Boot makes it surprisingly approachable — a handful of lines in application.properties and you're protected. The harder part is understanding why each piece exists, which is exactly what this post aimed to make crystal clear.