Spring-cloud-gateway: How to modify spring cloud gateway response headers

Created on 2 Jun 2019  ·  10Comments  ·  Source: spring-cloud/spring-cloud-gateway

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

Most helpful comment

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.

All 10 comments

Can you provide a complete, minimal, verifiable sample that reproduces the problem? It should be available as a GitHub (or similar) project or attached to this issue as a zip file.

@ryanjbaxter thanks, the core code is a filter https://github.com/spring-cloud/spring-cloud-gateway/files/3244970/code.txt ,but it can't modify header in a post filter,is it a right way writing like this?

That is not a complete working sample, it is just some code.

There should be no reason why a filter cannot modify a response header. We do this already
https://github.com/spring-cloud/spring-cloud-gateway/blob/master/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SetResponseHeaderGatewayFilterFactory.java

@ryanjbaxter it seems a route filter,can i modify a response header in a global post filter,thanks

yes

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

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.

Facing same issue, found any solution?

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ryanjbaxter picture ryanjbaxter  ·  6Comments

samtonyclarke picture samtonyclarke  ·  3Comments

dennis-menge picture dennis-menge  ·  4Comments

manishonline picture manishonline  ·  3Comments

aresa7796 picture aresa7796  ·  6Comments