undo
Go Beyond the Code
arrow_forward_ios

Managing dynamic subdomains in Spring Boot

July 30, 2024

There is an ongoing trend in the SaaS industry to use subdomains for clients (like client.mysass.com) to provide customized and isolated environments and a more "personalized" treatment. This approach also offers numerous benefits, including improved security, easier management of client-specific configurations, and enhanced user experience. This article provides a step-by-step guide on how to implement this feature in the context of an existing Spring Boot application.


Implementation

1. Setting up the DNS records

Assuming our domain is example.com and we have control over the DNS records, let’s begin by creating a wildcard record pointing to our application and point that entry to a load balancer or CDN

Type: CNAME
Domain: *.example.com
Alias: production-loadbalancer.example.com

This means that any request made to any subdomain of example.com (i.e. foo.example.com) that is not explicitly defined in a record will be routed to production-loadbalancer.example.com

2. Setting up the application

Next step is configuring the application that will handle the request. Let's assume that production-loadbalancer.example.com is a load balancer that distributes within a cluster of containers running a Spring Boot app. In the context of this application, let's start by creating a class that implements HandlerInterceptor. The preHandle method will be called on every request, before reaching a controller. Inside this method, we can inspect the HttpServletRequest, get the Host header and from it, extract the subdomain.


In this case, the manageSubdomain method encapsulates the logic that determines the outcome of the request based on the subdomain.

Finally, include the HostHandlerInterceptor component into the interceptor registry:


This approach offers a lot of flexibility in terms of managing subdomains at a high level, allowing the implementation to be tuned to handle more complex scenarios.


Conclusion

Using a subdomain per client in web applications enhances security, customization, and scalability. This simple solution demonstrates how we can create and manage dynamic subdomains programmatically, providing a robust solution for handling client-specific requirements in a multi-tenant architecture.

Facundo Juárez
Software Engineer & Solver

Start Your Digital Journey Now!

Which capabilities are you interested in?
You may select more than one.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.