spring-cloud / spring-cloud/spring-cloud-gateway

Variable expression as AddRequestHeader 'value' argument (e.g. with SpEL expression)

Open
#2,376 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

waiting for votes
Dominant language
Java
Stars
4.9k
Forks
3.5k
Avg merge
20h 57m
Merged PRs (30d)
8

Description

Is your feature request related to a problem? Please describe.
I'd like to use a variable expression to set the value of a request header with the AddRequestHeader filter (value argument) in my application.yml (instead of using Java DSL) ; variable means here that the expression is evaluated at runtime in the context of a request.

For example, set a header to a UUID (using a SpEL expression for example):

 - AddRequestHeader=X-Request-Id, #{ T(java.util.UUID).randomUUID().toString() }

More advanced (quick and dirty) example, assuming the ServerWebExchange object in the request context is put in the context of the SpEL expression evaluation as the variable named exchange:

 - AddRequestHeader=REMOTE_USER, #{ exchange.principal.block().getName() }

If possible, I would like the same feature for other gateway filters modifying or setting request/response attributes (AddRequestParameter, AddResponseHeader, etc.)

Describe the solution you'd like
I'd like to use simple variable expressions, as shown in the examples above. I suggested SpEL in the examples because this is the natural language for such expressions in Spring. But other syntaxes or languages are welcome as long as they can address the basic examples above at least.

Describe alternatives you've considered
Obviously the alternative is to use the Java DSL, but I consider it overkill for cases like this where a simple SpEL expression would suffice to assign the header value - or any other request/response attribute - in the yaml configuration.

Maybe it is already possible, but I don't see how to do it in the yaml.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the AddRequestHeader filter and how its value is parsed from application.yml, then compare that path with the Java DSL. Determine whether a runtime expression mechanism can be applied consistently to request and response attribute filters; done means a documented YAML example works for the requested header value and has coverage for the supported expression behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.