How to Change the Default Port in Spring Boot

Table of Contents

Spring Boot is a popular framework for building Java applications with a strong emphasis on simplicity and convention over configuration. By default, Spring Boot applications run on port 8080. However, there are scenarios where you might want to change the default port to avoid conflicts or align with your application’s requirements. In this article, we will discuss various methods to change the default port in a Spring Boot application.

Why Change the Default Port?

Changing the default port of a Spring Boot application can be necessary for several reasons:

  1. Port Conflicts: Other applications or services might be using the default port 8080, causing conflicts. Changing the port helps avoid such conflicts.
  2. Security: Running applications on non-standard ports can add an additional layer of security by making it harder for potential attackers to target commonly used ports.
  3. Compliance: Your organization’s policies or security guidelines might require applications to run on specific ports.

Changing Port Using application.properties or application.yml:

The easiest way to change the default port in a Spring Boot application is by configuring it through the application.properties or application.yml file. Here’s how you can do it:

  1. Using application.properties: Open the src/main/resources/application.properties file and add the following line:
   server.port=8081

Replace 8081 with your desired port number. Save the file.

  1. Using application.yml: If you’re using the application.yml file for configuration, add the following lines:
   server:
     port: 8081

Again, replace 8081 with the desired port.

Changing Port Using Command-Line Arguments:

You can also change the port by passing a command-line argument when running the Spring Boot application. Here’s how:

  1. Using Maven: Run the following command in your terminal:
   mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8081
  1. Using Gradle: For Gradle, use the following command:
   gradle bootRun --args='--server.port=8081'

Changing Port Programmatically:

If you need more dynamic control over the port configuration, you can set it programmatically in your Spring Boot application’s code. This can be helpful when you want to conditionally determine the port based on environment variables, configurations, or other runtime factors. Here’s an example of how to achieve this:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;

@SpringBootApplication
public class PortChangeApplication implements ApplicationListener<WebServerInitializedEvent> {

    public static void main(String[] args) {
        SpringApplication.run(PortChangeApplication.class, args);
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        int port = event.getWebServer().getPort();
        // Perform any conditional logic to determine the desired port
        // For example, you can read from a configuration file or environment variables
        // int desiredPort = ...;
        // event.getWebServer().setPort(desiredPort);
    }
}

Port Forwarding and Reverse Proxy Considerations:

When changing the default port of your Spring Boot application, it’s important to consider the broader network architecture in which your application operates. If your application is behind a firewall or NAT (Network Address Translation) setup, you might need to configure port forwarding to ensure that external traffic can reach your application.

Additionally, if you plan to expose your Spring Boot application to the internet, it’s a good practice to use a reverse proxy server like Nginx or Apache. A reverse proxy can handle tasks such as SSL termination, load balancing, and forwarding requests to your application running on a non-standard port. Let’s take a look at how you can set up a reverse proxy using Nginx:

  1. Install Nginx: Install Nginx on your server if it’s not already installed:
   sudo apt update
   sudo apt install nginx
  1. Configure Nginx: Create a new Nginx configuration file, for example, myapp.conf, in the /etc/nginx/conf.d/ directory:
   server {
       listen 80;
       server_name yourdomain.com;

       location / {
           proxy_pass http://127.0.0.1:8081;  # Change this to your Spring Boot app's IP and port
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
       }
   }

In this example, Nginx listens on port 80 and forwards requests to the Spring Boot application running on port 8081. Adjust the configuration to match your application’s setup.

  1. Restart Nginx: After making changes to the Nginx configuration, restart Nginx to apply the changes:
   sudo service nginx restart

Handling HTTPS with a Reverse Proxy:

If security is a concern, you should consider using HTTPS to encrypt the communication between clients and your Spring Boot application. You can obtain an SSL certificate and configure Nginx to handle SSL termination. Here’s how:

  1. Obtain an SSL Certificate: You can obtain an SSL certificate from a trusted certificate authority (CA) like Let’s Encrypt.
  2. Configure Nginx for SSL: Update your Nginx configuration to include the SSL settings and reference the SSL certificate files:
   server {
       listen 443 ssl;
       server_name yourdomain.com;

       ssl_certificate /etc/nginx/ssl/yourdomain.com.crt;
       ssl_certificate_key /etc/nginx/ssl/yourdomain.com.key;

       location / {
           proxy_pass http://127.0.0.1:8081;  
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
       }
   }
  1. Restart Nginx: After updating the Nginx configuration, restart Nginx again:
   sudo service nginx restart

By setting up a reverse proxy with Nginx and enabling HTTPS, you ensure that your Spring Boot application’s communication is secure and properly managed.

Conclusion:

Changing the default port of a Spring Boot application is just the beginning of ensuring your application’s accessibility and security. Consider the broader network architecture, potential firewall and NAT setups, and use a reverse proxy to manage incoming traffic and enhance security. By following the steps and guidelines provided in this article, you can confidently change the default port of your Spring Boot application while maintaining optimal performance, security, and accessibility.

Command PATH Security in Go

Command PATH Security in Go

In the realm of software development, security is paramount. Whether you’re building a small utility or a large-scale application, ensuring that your code is robust

Read More »
Undefined vs Null in JavaScript

Undefined vs Null in JavaScript

JavaScript, as a dynamically-typed language, provides two distinct primitive values to represent the absence of a meaningful value: undefined and null. Although they might seem

Read More »