Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
Allow Spring Boot Endpoint to be called only from Internal Network

Allow Spring Boot Endpoint to be called only from Internal Network

Posted on December 8, 2024 by Toma Velev

You can achieve an Spring Boot Endpoint to be called only from Internal Network by using the built-in support for security and configuring a filter to check the IP address of incoming requests. Here are the steps:

Step 1: Add dependencies

In your pom.xml file (if you’re using Maven) or build.gradle file (if you’re using Gradle), add the following dependencies:

Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

Gradle:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-security'
}

Step 2: Configure security

In your application.properties file, add the following configuration:

management.endpoints.web.exposure.include=*
spring.security.enabled=true

Step 3: Create a filter

Create a new Java class that implements Filter:

import org.springframework.stereotype.Component;
import javax.servlet.*;
import java.io.IOException;

@Component
public class InternalNetworkFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String remoteIp = httpRequest.getRemoteAddr();

        if (!isInternalNetwork(remoteIp)) {
            chain.doFilter(request, response);
            return;
        }

        chain.doFilter(request, response);
    }

    private boolean isInternalNetwork(String remoteIp) {
        // Define your internal network IP range here
        String[] internalIps = {"192.168.1.", "10."};

        for (String ip : internalIps) {
            if (remoteIp.startsWith(ip)) {
                return true;
            }
        }

        return false;
    }
}

Step 4: Configure the filter

In your application.properties file, add the following configuration:

spring.security.filter-chain-order=0

This will ensure that the filter is executed before any other Spring Security filters.

Step 5: Add a security configuration

Create a new Java class that implements WebSecurityConfigurerAdapter:

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.addFilterBefore(new InternalNetworkFilter(), AnyRequestMatcher.class);
    }
}

This will add the InternalNetworkFilter to the filter chain.

Step 6: Test

Start your Spring Boot application and use a tool like Postman or cURL to send requests from both internal and external networks. The endpoint should only be accessible from the internal network.

It is a common case your app to run behind a load balancer, nginx, apache proxy. In these cases the IP is passed not as request.getRemoteAddr(), but as a Header:
String ip = request.getHeader("X-Forwarded-For");
if (ip != null) {
response.setStatus(401);
return;
}

Note: This is a basic example and you may need to adjust it according to your specific requirements. Additionally, this approach assumes that the internal network IP range is known and can be configured in the filter.

I’ve got a small App to store a White List of IP addresses – to use across multiple apps: https://programtom.com/dev/product/application-level-ip-white-list-micro-service/

  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (84)
    • Flutter Apps (24)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Post Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)