Spring Boot
HTTPS
HTTP redirection
web security
redirect configuration

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:

java
1import org.springframework.context.annotation.Bean;
2import org.springframework.context.annotation.Configuration;
3import org.springframework.security.config.Customizer;
4import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5import org.springframework.security.web.SecurityFilterChain;
6
7@Configuration
8public class SecurityConfig {
9
10    @Bean
11    SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
12        http
13            .requiresChannel(channel ->
14                channel.anyRequest().requiresSecure()
15            )
16            .authorizeHttpRequests(auth -> auth.anyRequest().permitAll())
17            .httpBasic(Customizer.withDefaults());
18
19        return http.build();
20    }
21}

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:

yaml
server:
  forward-headers-strategy: framework

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:

yaml
1server:
2  port: 8443
3  ssl:
4    enabled: true
5    key-store: classpath:keystore.p12
6    key-store-password: changeit
7    key-store-type: PKCS12
8    key-alias: app

To listen on an insecure port only so you can redirect it, add a connector:

java
1import org.apache.catalina.connector.Connector;
2import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
3import org.springframework.boot.web.server.WebServerFactoryCustomizer;
4import org.springframework.context.annotation.Bean;
5import org.springframework.context.annotation.Configuration;
6
7@Configuration
8public class TomcatRedirectConfig {
9
10    @Bean
11    WebServerFactoryCustomizer<TomcatServletWebServerFactory> servletContainer() {
12        return factory -> factory.addAdditionalTomcatConnectors(httpConnector());
13    }
14
15    private Connector httpConnector() {
16        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
17        connector.setScheme("http");
18        connector.setPort(8080);
19        connector.setSecure(false);
20        connector.setRedirectPort(8443);
21        return connector;
22    }
23}

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.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design