Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X-Forwarded-Prefix not working when using MVC #3354

Open
NilsEngelbach opened this issue Apr 15, 2024 · 0 comments
Open

X-Forwarded-Prefix not working when using MVC #3354

NilsEngelbach opened this issue Apr 15, 2024 · 0 comments

Comments

@NilsEngelbach
Copy link
Contributor

Describe the bug
When using Spring Cloud Gateway MVC (2023.0.1) with Spring Boot (3.2.4) the forwarded requests are missing the X-Forwarded-Prefix Header. I have prepared a small comparison. The only possibility I found was setting the Header manually using the AddRequestHeader=X-Forwarded-Prefix, /foo/bar filter.

Shared Configuration

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2023.0.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>3.2.4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        [...]
    </dependencies>

MVC Configuration

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
        </dependency>
server:
  port: ${SERVER_PORT:8080}
  forward-headers-strategy: FRAMEWORK

spring:
  cloud:
    gateway:
      mvc:
        routes:
          - id: echo-service
            uri: <URL>
            filters:
              - StripPrefix=2
            predicates:
              - Path=/foo/bar/**

The forwarded Request ist missing the X-Forwarded-Prefix Header:

GET /baz HTTP/2.0

Host: <HOST>
X-Forwarded-For: 0:0:0:0:0:0:0:1
X-Forwarded-Host: localhost:8080
X-Forwarded-Port: 8080
X-Forwarded-Proto: http

REACTIVE Configuration

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
server:
  port: ${SERVER_PORT:8080}
  forward-headers-strategy: FRAMEWORK

spring:
  cloud:
    gateway:
      routes:
        - id: echo-service
          uri: <URL>
          filters:
            - StripPrefix=2
          predicates:
            - Path=/foo/bar/**
GET /baz HTTP/1.1

Host: <HOST>
X-Forwarded-For: 0:0:0:0:0:0:0:1
X-Forwarded-Host: localhost:8080
X-Forwarded-Port: 8080
X-Forwarded-Prefix: /foo/bar
X-Forwarded-Proto: http
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

2 participants