Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot server port range setting

Tags:

spring-boot

Is it possible to set an acceptable range for the server.port in the application.yml file for a spring boot application.

I have taken to setting server.port=0 to get an automatically assigned port rather than a hard coded one.

Our network ops people want to restrict the available range for this port assignment.

Any idea?

like image 820
EvilJinious1 Avatar asked Sep 13 '25 21:09

EvilJinious1


1 Answers

Following both user1289300 and Dave Syer, I used the answers to formulate one solution. It is supplied as a configuration that reads from the application.yml file for the server section. I supplied a port range min and max to choose from. Thanks again

@Configuration
@ConfigurationProperties("server")
public class EmbeddedServletConfiguration{

/*
    Added EmbeddedServletContainer as Tomcat currently. Need to change in future if  EmbeddedServletContainer get changed
 */
private final int MIN_PORT = 1100;
private final int MAX_PORT = 65535;
/**
 * this is the read port from the applcation.yml file
 */
private int port;
/**
 * this is the min port number that can be selected and is filled in from the application yml fil if it exists
 */
private int maxPort = MIN_PORT;

/**
 * this is the max port number that can be selected and is filled
 */
private int minPort = MAX_PORT;

/**
 * Added EmbeddedServletContainer as Tomcat currently. Need to change in future if  EmbeddedServletContainer get changed
 *
 * @return the container factory
 */
@Bean
public EmbeddedServletContainerFactory servletContainer() {
    return new TomcatEmbeddedServletContainerFactory();
}

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            // this only applies if someone has requested automatic port assignment
            if (port == 0) {
                // make sure the ports are correct and min > max
                validatePorts();
                int port = SocketUtils.findAvailableTcpPort(minPort, maxPort);
                container.setPort(port);
            }
           container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404"));
           container.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/403"));

        }
    };
}

/**
 * validate the port choices
 * - the ports must be sensible numbers and within the alowable range and we fix them if not
 * - the max port must be greater than the min port and we set it if not
 */
 private void validatePorts() {
     if (minPort < MIN_PORT || minPort > MAX_PORT - 1) {
         minPort = MIN_PORT;
     }

     if (maxPort < MIN_PORT + 1 || maxPort > MAX_PORT) {
         maxPort = MAX_PORT;
     }

     if (minPort > maxPort) {
         maxPort = minPort + 1;
     }
 }

}
like image 144
EvilJinious1 Avatar answered Sep 15 '25 19:09

EvilJinious1