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

Spring Cloud Gateway doesn't add X-Forwarded-Prefix header #314

Closed
RobMaskell opened this issue May 12, 2018 · 8 comments
Closed

Spring Cloud Gateway doesn't add X-Forwarded-Prefix header #314

RobMaskell opened this issue May 12, 2018 · 8 comments

Comments

@RobMaskell
Copy link

I have working zuul config and wanted to convert it to spring cloud gateway but I'm hitting a problem with the spring data rest / hateos response from my back-end api.

I have my gateway at http://localhost:8888 and access the api with http://localhost:8888/api/foo, the backend api server is at http://localhost:7777

The problem is that the /api is missing from the hateoas generated URIs in the response coming back.

The zuul config I'm replicating is as follows

application.properties

server.port=8888

zuul.routes.api.path=/api/**
zuul.routes.api.url=http://localhost:7777

zuul.add-proxy-headers=true
ribbon.eureka.enabled=false

The config for spring cloud gateway is as follows

application.properties

server.port=8888

spring.cloud.gateway.forwarded.enabled=true
spring.cloud.gateway.x-forwarded.enabled=true
spring.cloud.gateway.x-forwarded.forEnabled=true
spring.cloud.gateway.x-forwarded.protoEnabled=true

java api routes config

    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("api", r ->
                        r.path("/api/{segment}")
                            .filters(f -> f
//                                    .stripPrefix(1)
                                    .setPath("/{segment}")
                            )
                            .uri("http://localhost:7777")
                )
                .build();
    }

I've tried with and without the x-forwarded properties and it's the same with setPath and stripPrefix and I'm out of ideas now.

@RobMaskell RobMaskell changed the title Possible issue with forwarded headers Spring Cloud Gateway doesn't send X-Forwarded-Prefix header May 19, 2018
@RobMaskell
Copy link
Author

Did a bit more investigation comparing headers added by zuul and S-C-G and it seems the difference is the X-Forwarded-Prefix header isn't being added

@RobMaskell RobMaskell changed the title Spring Cloud Gateway doesn't send X-Forwarded-Prefix header Spring Cloud Gateway doesn't add X-Forwarded-Prefix header May 19, 2018
@RobMaskell
Copy link
Author

I've created a minimal app for another gateway issue with chains of proxies that I'll be opening but it also shows this issue https://github.com/RobMaskell/gatewayissues.

To reproduce, bootRun the 'api' and 'web' modules in the IDE and access http://locahost:8888/api/ and look at the resulting json response, note the lack of /api in the Spring Hateoas generated URIs (because it uses spring URI builder that picks up on x-forwarded-prefix).

Don't know if this is an enhancement or needs documenting as unsupported but I feel Spring projects should be consistent in support for headers especially in simple cases like the StripPrefix filter.

If considered a valid bug or enhancement then I can look at submitting a PR for StripPrefix at least.

@ryandawsonuk
Copy link
Contributor

ryandawsonuk commented Jun 20, 2018

I am also hitting this in Kubernetes (on AWS). I saw the same behaviour with nginx ingress before adding the annotation 'nginx.ingress.kubernetes.io/x-forwarded-prefix: "true"'. I wonder if the problem is just that XForwardedHeadersFilter doesn't included X-Forwarded-Prefix. I am trying to find out. (I notice from RobMaskell/gatewayissues that @RobMaskell was able to add a filter with "- AddRequestHeader=x-forwarded-prefix, /api" but I am using discovery locator so don't have a fixed path I can use.)

@RobMaskell
Copy link
Author

@ryandawsonuk I couldn't find any x-forwarded-prefix related code in the x-forwarded filter so I came to the conclusion that it was unsupported for now

@ryandawsonuk
Copy link
Contributor

There is a header called X-Original-URI that contains the path with the prefix in front. From this it is possible to extract the prefix. I tried overriding the XForwardedHeadersFilter class and found that it did then set the prefix and the links in the hateoas are then correct.

@RobMaskell
Copy link
Author

I don't remember seeing that header in my case so maybe that will just work for you and not be a generic solution?

@ryandawsonuk
Copy link
Contributor

Might be that it's an nginx thing. @RobMaskell Were you getting to the gateway via an ingress?

@RobMaskell
Copy link
Author

@ryandawsonuk an Ingress yes but a garden variety GCP HTTP LB Ingress

@spencergibb spencergibb added this to the 2.0.2.RELEASE milestone Aug 10, 2018
Johnny850807 pushed a commit to Johnny850807/spring-cloud-gateway that referenced this issue Dec 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants