Spring server.forward-headers-strategy NATIVE vs FRAMEWORK
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the world of web development, configuring the behavior of server headers is a critical task that ensures the secure and efficient relay of requests. Spring Framework offers several strategies to handle forward headers, which are essential when dealing with proxy servers. Among these strategies, NATIVE and FRAMEWORK stand out, each serving different purposes depending on the use case. This article will delve into these strategies, explore their differences, and provide insights into when each should be used.
Understanding Forward Headers
Forward headers are used by proxy servers to forward information about the original incoming request to the backend services. These headers can include information such as the client’s IP address, protocol, host, and port. Proper handling of these headers is crucial for accurate request processing.
Forward Headers Strategies in Spring
Spring Boot's server.forward-headers-strategy property dictates how to interpret these forward headers. This configuration is essential, especially when your application runs behind a reverse proxy. The two primary strategies available are NATIVE and FRAMEWORK.
NATIVE Strategy
The NATIVE strategy relies on the underlying servlet container to process the Forwarded or X-Forwarded-* headers. This approach is beneficial when using servlet containers that have built-in support for handling these headers, such as Tomcat or Jetty.
Advantages:
- Utilizes Native Capabilities: By leveraging the servlet container’s built-in capabilities, performance can be optimized, and configuration can be simplified.
- Seamless Integration: Works well with popular servlet containers that are often pre-configured to handle these headers.
Disadvantages:
- Dependency on Container: The application becomes reliant on the underlying container to properly configure and handle these headers.
- Limited to Support Provided by Container: May not support custom logic or require additional configuration outside the application scope.
FRAMEWORK Strategy
The FRAMEWORK strategy allows Spring itself to resolve the forwarding headers, regardless of the capabilities of the underlying servlet container. This is particularly useful when the servlet container does not natively support these headers or when custom handling is required.
Advantages:
- Consistent Behavior Across Containers: Ensures consistent handling of headers across different servlet containers.
- Customizability: Allows developers to tailor the handling of headers according to application-specific needs.
Disadvantages:
- Potential Overhead: Since Spring itself processes the headers, there could be slight performance overhead compared to native handling.
- Complex Configuration: May require additional configuration and understanding of how Spring processes these headers.
How to Choose Between NATIVE and FRAMEWORK
The choice between NATIVE and FRAMEWORK depends on several factors, including the servlet container being used, the level of customization required, and specific application needs.
Considerations:
- When to Use NATIVE:
- Your servlet container has robust support for forward headers.
- You are looking for out-of-the-box, efficient handling without extensive configuration.
- You are satisfied with the default behavior provided by your container.
- When to Use FRAMEWORK:
- Consistent experience across different servlet containers is necessary.
- The application needs to apply custom logic to header processing.
- The servlet container does not support or has limited handling of forward headers.
Configuration Example
Using NATIVE Strategy

