1
votes

I have Zuul + Eureka + Spring Boot Service Endpoint + Hateoas response configuration. When I access the service through Zuul Gateway, the resource links in the response are direct links to the service endpoints, shouldn't they be the Gateway links? What am i missing here?

Gateway Endpoint : http://localhost:8762/catalog/products/10001 Direct Service Endpoint : http://localhost:8100/products/10001

application.properties for Zuul

spring.application.name=zuul-server
eureka.client.service-url.default-zone=http://localhost:8761/eureka/

# Map paths to services
zuul.routes.catalog-service=/catalog/**
zuul.addProxyHeaders=true

Actual Response on Gateway Endpoint : http://localhost:8762/catalog/products/10001

{
  "title" : "The Title",
  "description" : "The Description",
  "brand" : "SOME BRAND",
  "price" : 100,
  "color" : "Black",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8100/products/10001"
    }
  }
}

Expected Response should have Gateway URL in href

{
  "title" : "The Title",
  "description" : "The Description",
  "brand" : "SOME BRAND",
  "price" : 100,
  "color" : "Black",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8762/catalog/products/10001"
    }
  }
}
2

2 Answers

5
votes

I've got this issue and resolved it via this post on github

The gist is

spring-boot <=2.1

@Bean
ForwardedHeaderFilter forwardedHeaderFilter() {
    return new ForwardedHeaderFilter();
}

spring-boot >= 2.2

server.use-forward-headers=true
1
votes

While @Nikhil said he fixed by just adding @Bean, in my case is just the opposite:

I just added forward-headers-strategy: FRAMEWORK (currently, server.use-forward-headers is deprecated) and it worked for me that way.

Thank you @Zipster!

Additional info:

Property server.use-forward-headers accepts three possible values:

  • NONE
  • NATIVE
  • FRAMEWORK

I tested the three options to check the differences.

My Zuul gateway (port 8080) routes are configured as it follows:

zuul:
  prefix: /api/v0
  sensitive-headers: Cookie,Set-Cookie # Allow Authorization header forwarding
  routes:
    api-v0-questions:
       path: /questions/**
       service-id: api-v0-questions
       strip-prefix: false

NATIVE - URL points to gateway and stripping the /api/v0 (top prefix):

"_links": {
  "self": {
    "href": "http://localhost:8080/questions/5f6fa0300ec87b34b70393ca"
  }

FRAMEWORK - URL points to gateway and DOES NOT strip the /api/v0 prefix:

"_links": {
  "self": {
    "href": "http://localhost:8080/api/v0/questions/5f6fa0300ec87b34b70393ca"
  }

NONE - URL points to service, just like as adding no property at all:

"_links": {
  "self": {
    "href": "http://localhost:8081/questions/5f6f96ba0ec87b34b70393b2"
  }