How to modify spring cloud gateway response headers
See original GitHub issueI 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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:10 (3 by maintainers)
Top Related StackOverflow Question
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.
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