Add HTTP to HTTPS redirect configuration using embedded Tomcat
- HTTP connector on port 8080 redirects to HTTPS (8443). - Ensures users accessing http:// are forwarded to https:// - No need for external proxy (e.g., Nginx) for redirection.
This commit is contained in:
+23
@@ -0,0 +1,23 @@
|
|||||||
|
package com.skycrate.backend.skycrateBackend.config;
|
||||||
|
|
||||||
|
import org.apache.catalina.connector.Connector;
|
||||||
|
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
|
||||||
|
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class HttpToHttpsRedirectConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> containerCustomizer() {
|
||||||
|
return factory -> {
|
||||||
|
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
|
||||||
|
connector.setScheme("http");
|
||||||
|
connector.setPort(8080); // HTTP port
|
||||||
|
connector.setSecure(false);
|
||||||
|
connector.setRedirectPort(8443); // HTTPS port
|
||||||
|
factory.addAdditionalTomcatConnectors(connector);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user