I want my SpringBoot app to redirect request sent to port 81 to port 8080 (which is the port the application binds to). However the app fails on startup, as you are not allowed to use a privileged port.
I know there is a solution using root access or by using something like apache2 to redirect, but if possible I'd like a programmatic solution using my SpringBoot application only, no external configuration.
This is my configuration file for redirecting ports (it works if I change 81 to any port over 1024):
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class EmbeddedTomcatConfiguration {
private static final String PROTOCOL_HTTP = "HTTP/1.1";
private static final String SCHEME_HTTP = "http";
private static final long ASYNC_TIMEOUT = 20000;
private static final int REDIRECT_PORT = 8080;
private static final int LISTENING_PORT = 81;
@Bean
public EmbeddedServletContainerFactory servletContainer() {
final TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
tomcat.addAdditionalTomcatConnectors(createConnector());
return tomcat;
}
private Connector createConnector() {
Connector connector = new Connector(PROTOCOL_HTTP);
connector.setScheme(SCHEME_HTTP);
connector.setPort(LISTENING_PORT);
connector.setRedirectPort(REDIRECT_PORT);
connector.setSecure(false);
connector.setAsyncTimeout(ASYNC_TIMEOUT);
return connector;
}
}
Aucun commentaire:
Enregistrer un commentaire