How to modify spring cloud gateway response headers

See original GitHub issue

I am trying to modify a header of response in a post filter of gateway,the filter handle a cors problem which would filt websockt service ,the websockt service is a micro-service which must been decorated with cors configuration,so a websockt request will get a response with multiple header like “Access-Control-Allow-Origin”, to solve this question,i must modify the response header of the key “Access-Control-Allow-Origin”。However ,when i do this, a error occured:

java.lang.UnsupportedOperationException: null at org.springframework.http.ReadOnlyHttpHeaders.set(ReadOnlyHttpHeaders.java:99) ~[spring-web-5.1.6.RELEASE.jar:5.1.6.RELEASE] at com.apigw.filter.CORSFilter.lambda$filter$0(CORSFilter.java:84) ~[classes/:na] at reactor.core.publisher.MonoRunnable.call(MonoRunnable.java:73) ~[reactor-core-3.2.8.RELEASE.jar:3.2.8.RELEASE]

It seems the response header cannot be modifed in post filter,the following is my code,please tell me a way to solve this problem.

code.txt

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
shi98013commented, Nov 1, 2019

For those that find this when searching for similar issue… I too was experiencing the UnsupportedOperationException when I added a post filter to an existing global filter which had an order that caused the post filter to action to occur after the response had been sent.

0reactions
shi98013commented, May 20, 2020

I suppose your issue may have been different than mine, but when I turned on trace logging I saw that my filter was executing after the response was sent and so I gave the filter a different order value that put it in the right order. Here is a link to someone asking about ordered filters that may provide more insight: https://github.com/spring-cloud/spring-cloud-gateway/issues/1341

Read more comments on GitHub >

github_iconTop Results From Across the Web

Question. How can i modify/add response headers ... - GitHub
Hi, Could you please help me for how to modify the response headers from Global Filter. When i use, serverWebExchange.getResponse().
Read more >
Spring Cloud Gateway
Route filters allow the modification of the incoming HTTP request or outgoing HTTP response in some manner. Route filters are scoped to a...
Read more >
Spring Gateway - Adding of default Request and Response ...
Spring Cloud Gateway doesn't support default filters via Beans. https://github.com/spring-cloud/spring-cloud-gateway/issues/263.
Read more >
Spring Cloud Gateway: Route and Mutate Request Headers
With Spring Cloud Gateway, we had been able to route and mutate the request with minimal configuration and a single filter. As usual,...
Read more >
Spring Cloud Gateway WebFilter Factories - Baeldung
Likewise, we can modify response headers by using add (AddResponseHeader), set or replace (SetResponseHeader), remove (RemoveResponseHeader) and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found