Cannot modify headers on After Filter when response is created with `ServerResponse.ok().build()`

オープン
#4,168 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
java, spring, spring-boot
領域
api, backend

調査の方向性

スタックトレースで特定された AfterFilterFunctions.addResponseHeader() から開始し、fooRoute ServerResponse.ok().build() ルートと barRoute HandlerFunctions.http() ルートの違いを再現します。このパスでレスポンスヘッダーがどのように公開されるかを調査し、既存の after-filter の動作を検証します。ServerResponse の直接レスポンスに対して UnsupportedOperationException を発生させずに X-Test が追加され、proxy ルートが引き続き動作すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

waiting-for-triage

Describe the bug
When a route handler returns a ServerResponse created via ServerResponse.ok().build(). The headers exposed to ServerResponse.headers() are immutable (ReadOnlyHttpHeaders).
Attempting to modify response headers inside an After Filter (even with predefined AfterFilterFunctions.addResponseHeader() function) throws UnsupportedOperationException with stacktrace:

java.lang.UnsupportedOperationException
        at org.springframework.http.ReadOnlyHttpHeaders.addAll(ReadOnlyHttpHeaders.java:102) ~[spring-web-6.2.18.jar:6.2.18]
        at org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.lambda$addResponseHeader$0(AfterFilterFunctions.java:59) ~[spring-cloud-gateway-server-mv
c-4.3.4.jar:4.3.4]
--- snip ---

However, the same .after() filter works correctly when using predefined handlers such as HandlerFunctions.http()

Expected behavior
AfterFilterFunctions.addResponseHeader() (and any After Filter Functions that modifies headers) should be able to modify response headers consistently for ServerResponse returned from route handlers.

Alternatively, if immutable headers are intentional, the framework should avoid mutating response.headers() directly inside AfterFilterFunctions.addResponseHeader().

Sample
Minimal, reproducible code example

  1. fooRoute (failing route) returns 500 Internal Server Error
public RouterFunction<ServerResponse> fooRoute() {
    return route("fooRoute")
            .GET(path("/foo"), req -> ServerResponse.ok().build())
            .after(addResponseHeader("X-Test", "TestFoo"))
            .build();
}
  1. barRoute (working route) returns 200 OK with expected response header
public RouterFunction<ServerResponse> barRoute() {
    return route("barRoute")
            .GET(path("/bar"), http())
            .before(uri("http://httpbin.org"))
            .before(setPath("/get"))
            .after(addResponseHeader("X-Test", "TestBar"))
            .build();
}

Java version: 21
Spring boot: 3.5.14
Spring cloud version: 2025.0.2
Spring cloud gateway web MVC: 4.3.4

This issue appears specific to response created directly from route handlers, since proxied responses trough HandlerFunctions.http() behave as expected.

主要言語
Java
スター
4.9k
フォーク
3.5k
平均マージ
1日 13時間
マージ済み PR(30日)
14

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

spring-cloud/spring-cloud-gateway のほかの issue

spring-cloud/spring-cloud-gateway の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。