How to inject a variable placeholder `${}` in the spring gateway filters configuration? - spring-boot

How to inject a variable placeholder ${} in the spring gateway filters configuration?
foo:
bar:
uri: /coucou
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: https://example.org
filters:
- PrefixPath=$\\{foo.bar.uri}
NB:
I've tried $\{foo.bar.uri} and ${foo.bar.uri}
I know it's doable programmatically

As #spencergibb highlighted, it's working by using args subproperty:
filters:
- name: PrefixPath
args:
prefix: ${foo.bar.uri}
see gist.github.com/spencergibb/873f239529f79cb784d4eab3a9ddc4a6

Related

Spring Cloud Gateway - RewriteLocationResponseHeader

I have the following route inside my Spring Cloud Gateway configuration:
- id: pgadmin
uri: lb://pg-admin-service
predicates:
- Path=/pgadmin/**
filters:
- RewritePath=/pgadmin(?<segment>/?.*), $\{segment}
- RewriteLocationResponseHeader=AS_IN_REQUEST, Location, , <---- this line is incorrect
The request comes in on http://10.0.0.100/pgadmin/ as an unauthenticated user. The application returns a response containing a location header with the value:
http://10.0.0.100/login
The browser tries to rediect to that URL. Instead, it should redirect to:
http://10.0.0.100/pgadmin/login
What is the correct value for the RewriteLocationResponseHeader in the route definition?
Many thanks
JT
Try something this if you want to rewrite your path:
- id: docs
uri: lb://auth-service
predicates:
- Path=/my-auth/**
filters:
- RewritePath=/my-auth(?<segment>/?.*), $\{segment}
or to simply forward the request onto your service:
- id: docs
uri: lb://auth-service
predicates:
- Path=/my-auth/**

Spring gateway - how to strip prefix for all routes only once

I am using configuration mode. Where is multiple (20) routes. But my server is accessible behind the URL PATH prefix http://prefixHere/method:port?property=value due Firewall and this can not be changed.
So when I have 20 of different methods (each ends in other service) then I must define 20 times.
I want to define the StripPrefix only once. This was working in previous Zuul gateway. How to do in cloud gateway?
Here is my config:
spring:
cloud:
gateway:
discovery:
locator:
lower-case-service-id: true
enabled: true
routes:
- id: auth-service
uri: lb://server-auth
predicates:
- Path=/prefixHere/auth/**
filters:
**- StripPrefix=1**
- id: operation-service
uri: lb://operation-service
predicates:
- Path=/prefixHere/operation/**
filters:
**- StripPrefix=1**
Yes, use "default-filters:" and it will be applied to all routes at once.
spring:
application:
name: gateway
cloud:
gateway:
default-filters:
- StripPrefix=1
E.g:
spring:
application:
name: GATEWAY-SERVICE
cloud:
gateway:
default-filters:
- StripPrefix=1
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: department-service
uri: lb://department-service
predicates:
- Path=/api/departments/**
- id: user-service
uri: lb://USER-SERVICE
predicates:
- Path=/api/users/**
Reference: Introduction to spring cloud gateway

Spring Cloud Gateway setup multiple virtual hosts

I'm on spring-cloud-starter-gateway (Hoxton.SR5) trying to support multiple hosts but not having luck.
I would like http://en.portal.com/common route to port 80 and http://us.portal.com/common route to port 81.
- id: host_route_en
uri: lb://127.0.0.1:80
predicates:
- Host=en.portal.com
- Path=/common/**
- id: host_route_us
uri: lb://127.0.0.1:81
predicates:
- Host=us.portal.com
- Path=/common/**
Could you guide how to achieve that.
Change it to
spring:
cloud:
gateway:
routes:
- id: host_route_en
uri: http://127.0.0.1:80
predicates:
- Host=en.portal.com
- Path=/common/**
- id: host_route_us
uri: http://127.0.0.1:81
predicates:
- Host=us.portal.com
- Path=/common/**
Reference document: https://docs.spring.io/spring-cloud-gateway/docs/2.2.5.RELEASE/reference/html/#shortcut-configuration

Spring Cloud Gateway : disable default routes

I'm using spring cloud to manage my microservices.
For security reasons, for one specific microservice (name it ms_secure), I want to use custom route choose a specific microservice version depending on client IP.
My gateway config looks like this:
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: ms_secure_v1
uri: lb://ms_secure_v1
predicates:
- Path=/ms_secure/**
filters:
- RewritePath=/ms_secure/(?<segment>.*), /$\{segment}
- name: <my filter>
args:
xForwardedForHeaderName: X-Forwarded-For
hosts:
- <IP1>
- <IP2>
- id: ms_secure
uri: lb://ms_secure_v2
predicates:
- Path=/ms_secure/**
filters:
- RewritePath=/ms_secure/(?<segment>.*), /$\{segment}
- name: <my filter>
args:
xForwardedForHeaderName: X-Forwarded-For
hosts:
- <IP3>
- <IP4>
When when requesting /ms_secure:
IP1 and IP2 will be redirected to ms_secure_v1
IP3 and IP4 will be redirected to ms_secure_v2
My problem is that all my clients will also be able to access directly ms_secure_v1 or ms_secure_v2 by using the default routes:
http:///ms_secure_v1/...
http:///ms_secure_v2/...
I tried to disable these routes by using SetStatus GatewayFilter:
- id: setstatusstring_route
uri: lb://ms-gateway
predicates:
- Path=/ms_secure_v**
filters:
- SetStatus=403
But this route is not matched.
Is there a way to disable these default routes in spring gateway?
The following creates routes in gateway based on services registered:
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
Set it to false (which is the default), if you don't want this.

Spring cloud Gateway and Consul Server

Please help.
I have Spring Cloud Gateway and Consul Server. In Spring Cloud Gateway i'm use "cloud:gateway:discovery:locator:enabled:true". I can send requests for services registered in the Consul (ServiceName). For example URL "/ServiceName/foo/bar".
Can I customize Spring Cloud Gateway so that the queries would look like /foo/ bar/ServiceName/baz ?
Yes you can
spring.cloud.gateway.discovery.locator.predicates[0].name: Path
spring.cloud.gateway.discovery.locator.predicates[0].args[pattern]: "'/foo/bar/'+serviceId+'/**'"
spring.cloud.gateway.discovery.locator.filters[0].name: RewritePath
spring.cloud.gateway.discovery.locator.filters[0].args[regexp]: "'/' + serviceId + '/foo/bar/(?<remaining>.*)'"
spring.cloud.gateway.discovery.locator.filters[0].args[replacement]: "'/${remaining}'"
My version for application.yml
spring:
cloud:
gateway:
locator:
enabled: true
predicates:
- name: Path
args:
pattern: "'/foo/bar/' + serviceId + '/**'"
filters:
- name: RewritePath
args:
regexp: "'/foo/bar/' + serviceId + '/(?<remaining>.*)'"
replacement: "'/foo/bar/' + serviceId + '/${remaining}'"

Resources