DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Micronaut vs Spring Boot: A Detailed Comparison
  • High-Performance Reactive REST API and Reactive DB Connection Using Java Spring Boot WebFlux R2DBC Example
  • Spring Boot 3.2: Replace Your RestTemplate With RestClient
  • Getting Started With Boot Spring 3.2.0: Building a Hello World REST API With NoSQL Integration

Trending

  • Event-Driven Pipelines With Apache Pulsar and Go
  • Building a Spring AI Assistant With MCP Servers: A Step-by-Step Tutorial
  • Slopsquatting: Building a Scanner That Catches AI-Hallucinated Packages Before They Reach Production
  • Your AI Agent Tests Are Passing, But Your Agent Is Still Broken
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Developing Secure REST API Using Spring Boot SSL Bundle Feature

Developing Secure REST API Using Spring Boot SSL Bundle Feature

Learn how to secure REST APIs in Spring Boot using SSL and self-signed certificates. Set up HTTPS on the server and configure a client to consume secure endpoints.

By 
Jiwan Gupta user avatar
Jiwan Gupta
·
Jul. 18, 25 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
6.9K Views

Join the DZone community and get the full member experience.

Join For Free

Secure Sockets Layer (SSL) is a key component in securing communication between systems, especially in layered or service-oriented architectures.

In such environments, a typical Spring Boot service might expose a REST endpoint, which is then consumed by another Spring Boot service acting as a client. When a host service exposes a secure endpoint, it must ensure that only authorized services are allowed to connect.

There are several ways to secure REST endpoints, including certificate exchange, JWT tokens, and OAuth. In this article, we’ll focus on certificate-based API protection using a self-signed certificate.

The diagram below illustrates the steps involved in establishing secure communication.

 

Diagram illustrating the steps involved in establishing secure communication.


Hosting a Secure API in Spring Boot

To host a secure REST API using Spring Boot, the service needs to go through a few key steps. First, you must generate a certificate signing request (CSR) and have it signed by a trusted Certificate Authority (CA).

As part of this process, you'll typically receive three certificates:

  • The host certificate
  • An intermediate certificate
  • A root certificate

These three certificates are then combined to form a certificate chain, which ensures the client can verify the entire trust path—from your service all the way to the root authority.

 Certificate viewer

 


For this article, we won’t be getting a certificate signed by a trusted authority. Instead, we’ll create a self-signed certificate. Self-signed certificates are commonly used to host and test HTTPS APIs in non-production environments. For production, however, it's best practice to use certificates signed by a trusted Certificate Authority (CA).

Steps to Create a Self-Signed Certificate

1. Generating a Keystore File

A keystore file acts like a secure database for storing cryptographic keys and certificates. It can be in either .JKS (Java Keystore) format or .P12/.PFX (PKCS12) format.

To generate a keystore, we’ll use the keytool command, which comes bundled with the JDK installation.

 

keytool -genkeypair -alias mylocalsslapp -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore mysslapp.p12 -validity 365

 

  • Keytool referes to keytool.exe and it is present in %JAVA_HOME%\bin folder
  • <genkeypair> command to generate cryptographic keys
  • <alias> Alias name of the entry to process
  • <keyalg> algorithm used for encryption and decryption
  • <keysize> Key bit size
  • <storetype> keystore type, this could be PKCS12 or JKS
  • <keystore> name of the keystore file
  • <validity> Validity number of days

 

While creating a certificate, the following details need to be provided, which will be validated by certificate authority to sign your certificate request.

Cypher
 
keytool -genkeypair -alias mylocalsslapp -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore mysslapp.p12 -validity 365

 Certificate request


Once this command runs successfully, you’ll see the mysslapp.p12 file created in the current working directory.


mysslapp.p12 file created 

 

You can view the contents of the generated certificate using the following command:


contents of the generated certificate 


Now your certificate is ready to be used in the Spring Boot server application.

SSL Configuration on the Server

Spring Boot makes it easy to configure and expose HTTPS APIs. With the recent introduction of SSL Bundles, it’s now even simpler to use and manage custom SSL trust material like keystores, certificates, and private keys. An SSL Bundle allows you to apply SSL configurations across one or more connections using standard Spring Boot APIs.

Create a REST Service

Let’s start by creating a simple userService that will return some static user data.

Java
 
@RestController
 @RequestMapping("/UserService")
 public class UserController {

     @GetMapping("/userDetails")
     public Map<String,String> getUserData(){
         Map<String,String> userMap = new HashMap<>();
         userMap.put("firstName","Bob");
         userMap.put("lastName","Wilson");
         userMap.put("department","security");
         userMap.put("phoneNumber","123-456-7890");

         return userMap;
}

}


Configure SSL Properties With Keystore

Add the following entries to the application.properties file of your Spring Boot server application to enable HTTPS using the keystore:


Properties files
 
server.port=8443
server.ssl.bundle=sslbundle
spring.ssl.bundle.jks.sslbundle.key.alias=mylocalsslapp
spring.ssl.bundle.jks.sslbundle.keystore.location=classpath:mysslapp.p12
spring.ssl.bundle.jks.sslbundle.keystore.password=myappsecret
spring.ssl.bundle.jks.sslbundle.keystore.type=PKCS12


Provide the same keystore password you used while creating the keystore in the earlier step. Then, copy the mysslapp.p12 certificate into the resources folder of your Spring Boot project.

Once these steps are done, you’re ready to start your microservice and test the /UserService endpoint from your browser.

When you navigate to the secure endpoint using the URL below, your browser may show a warning. Simply click on “Accept the Risk” (or similar) to proceed. You should then see the response returned by the service, confirming that your API is now accessible over HTTPS.

At this point, you’ve successfully set up a secure REST service using a self-signed certificate.

You can find the corresponding code for this example on GitHub at the following location:

 

corresponding code for this example on GitHub at the following location


Calling a Secure API from a Spring Boot Client Service

Now that your secure endpoint is up and running, the next step is to configure a Spring Boot client service that can call this HTTPS endpoint. To do this, the client must trust the certificate used by the server.

Export the Certificate from the Server

The server application needs to provide its certificate to the client. This certificate will be used by the client to validate the server when making secure API calls.

Use the following command on the server to export the certificate from the keystore:

keytool -exportcert -alias mylocalsslapp -keystore mysslapp.p12 -storetype PKCS12 -storepass ******  -file client.crt -rfc

 

This step will create a client.crt file in the location where the command is executed. This certificate file should be shared with the client application.

Configure Certificate in Client Service

Once the client receives the certificate, they should copy the .crt file into the resources folder of the client application.

Next, add the following entry to the application.properties file of the client service:

Properties files
 
spring.ssl.bundle.pem.clientbundle.truststore.certificate=classpath:client.crt


 If you are using RestTemplate to call a secure REST endpoint, you can create the RestTemplate instance using an SSL bundle as shown below:

Java
 
@Service
public class MyService {
    private final RestTemplate restTemplate;

    public MyService(RestTemplateBuilder restTemplateBuilder, SslBundles sslBundles) {
        this.restTemplate = restTemplateBuilder.sslBundle(sslBundles.getBundle("clientbundle")).build();
    }

    public RestTemplate getRestTemplate() {
        return restTemplate;
    }
}

 

Now, let’s expose another endpoint in the client service, which will internally call the secure endpoint hosted by the server application.

Java
 
@RestController
@RequestMapping("/UserService")
public class UserController {

    @Autowired
    private MyService myService;


    @GetMapping("/userDetails")
    public String getUserData(){
        return myService.getRestTemplate().getForObject("https://localhost:8443/UserService/userDetails", String.class);
    }

}


 Let’s call the client endpoint, which acts as the consumer of the secured server endpoint.


call the client endpoint, which acts as the consumer of the secured server endpoint


With this example, we successfully set up a server application that hosts a secured HTTPS endpoint, and a client application that securely consumes that endpoint.

You can find the complete code for both the server and client applications in the following GitHub repository.


API REST Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Micronaut vs Spring Boot: A Detailed Comparison
  • High-Performance Reactive REST API and Reactive DB Connection Using Java Spring Boot WebFlux R2DBC Example
  • Spring Boot 3.2: Replace Your RestTemplate With RestClient
  • Getting Started With Boot Spring 3.2.0: Building a Hello World REST API With NoSQL Integration

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook