Redirect HTTP to HTTPS in Spring Boot
Codemia Knowledge Hub ·
How you redirect HTTP to HTTPS in Spring Boot depends on where TLS terminates. If a reverse proxy, load balancer or ingress handles TLS, redirect there and configure forwarded headers in the app. If the Spring Boot process terminates TLS itself, use Spring Security's requiresSecure() or add an HTTP connector that redirects to the HTTPS port.
Decide where the redirect belongs
In production the best place to force HTTPS is often the edge layer. Behind Nginx, Apache, an AWS load balancer or a Kubernetes ingress, redirecting there is simpler and keeps insecure requests from travelling deeper into the stack.
An application-level redirect still makes sense when:
- the embedded server terminates TLS directly
- you want app-level enforcement during local or simple deployments
- you need Spring Security rules to require secure channels
The common mistake is assuming the application can detect the original scheme without proxy header configuration.
Redirect with Spring Security
With Spring Security in the stack, the cleanest application-level rule is to require secure channels:
Insecure requests are then redirected to HTTPS, as long as the application can correctly determine the incoming scheme.
Configure forwarded headers behind a proxy
When TLS terminates at a proxy, Spring Boot must trust the forwarded scheme. Otherwise the app may treat every request as plain HTTP (or HTTPS) and redirect wrongly. A common setting is:
This makes the framework honor forwarded headers such as X-Forwarded-Proto where the deployment supports them. Without it, a reverse proxy setup can cause:
- redirect loops
- wrong absolute URLs
- incorrect security decisions
Many "works on localhost" guides leave this step out.
Direct TLS in embedded Tomcat
If the app handles TLS itself, configure the secure connector, and optionally an HTTP connector that redirects to the HTTPS port. In application.yml:
To listen on an insecure port only so you can redirect it, add a connector:
This suits smaller deployments where the app owns both ports.
What else an HTTPS migration needs
Working redirects are not the whole job. Also consider:
- HSTS headers
- absolute URL generation
- secure cookies
- health checks and internal traffic
Spring Security adds HSTS by default on secure responses, which keeps browsers on HTTPS after the first secure visit.
Mistakes that break the redirect
- Configuring redirects in the app while TLS terminates at a proxy whose forwarded headers are not trusted. This usually causes redirect loops or broken links.
- Serving both HTTP and HTTPS indefinitely with no redirect, leaving users on insecure URLs depending on how they arrived.
- Forgetting that local development and production may need different setups. Embedded Tomcat can be fine locally while production enforcement happens at ingress.
- Confusing transport security with authentication. HTTPS protects the connection but does not replace normal auth and authorization rules.
Related reading
- Spring Boot REST API - request timeout?
- Spring Boot Rest Controller how to return different HTTP status codes?
- Spring boot REST validation error response
- Spring Boot Swagger UI. Set JWT token
- Spring Boot Security CORS
- Spring boot Security Disable security
- Spring Boot Remove Whitelabel Error Page
- Spring Boot request header return null value
System design interview problems on this topic
- Design a Load Balancer: solution and walkthrough
- Design a Domain Name System: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.