spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

How to modify spring cloud gateway response headers

Open DarrenJiang1990 opened this issue 7 years ago • 10 comments

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

DarrenJiang1990 avatar Jun 02 '19 16:06 DarrenJiang1990

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 avatar Jun 04 '19 00:06 ryanjbaxter

@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?

DarrenJiang1990 avatar Jun 04 '19 06:06 DarrenJiang1990

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 avatar Jun 05 '19 00:06 ryanjbaxter

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

DarrenJiang1990 avatar Jun 06 '19 09:06 DarrenJiang1990

yes

ryanjbaxter avatar Jun 13 '19 12:06 ryanjbaxter

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.

spring-projects-issues avatar Jun 20 '19 12:06 spring-projects-issues

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.

spring-projects-issues avatar Jun 27 '19 12:06 spring-projects-issues

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.

shi98013 avatar Nov 01 '19 23:11 shi98013

Facing same issue, found any solution?

thirumalx avatar May 20 '20 04:05 thirumalx

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

shi98013 avatar May 20 '20 04:05 shi98013