Member-only story

Run Spring Boot app on 2 ports

Karanbir Singh
2 min readDec 22, 2024

I use Spring boot almost daily and I had a very particular case to run a single Spring boot application on multiple ports.

To be specific I required to run same app on two ports simultaneously:-

➫ TLS = 8453

➫ MTLS = 8443

I was not sure how to do it until quite recently when I figured out an option using the config shared below:-

Main set of config and beans

// for the normal http port
@Bean
@ConditionalOnProperty(value = "server.second.secured", havingValue = "false")
public ServletWebServerFactory servletContainer(@Value("${server.second.server.port}") int httpPort) {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setPort(httpPort);

TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}

// for https port
@Bean
@ConditionalOnProperty(value = "server.second.secured", havingValue = "true", matchIfMissing = true)
public ServletWebServerFactory servletContainerSecured(@Value("${server.second.server.port}") int httpsPort,
ServerProperties serverProperties) {
try {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL)…

--

--

Karanbir Singh
Karanbir Singh

Written by Karanbir Singh

API developer + Web Application developer + Devops Engineer = Full Stack Developer

No responses yet