Improve TokenRelay filter documentation when Gateway acts as a BFF

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

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

@ruthst00 がすでに取り組んでいます。

2026年11月10日 から。

  • #4367 @ruthst00 による — オープン

評価

難易度
2/5
見積もり時間
半日
初心者へのやさしさ
58/100
issue の種類
ドキュメント
明瞭さ
おおむね明確
活発さ
活発
技術スタック
java, spring-boot

調査の方向性

The target is the TokenRelay filter reference page for the WebMVC gateway, which is documentation, not code. Read the existing filter section, then compare it with the reporter's SecurityFilterChain example, which sets oauth2Login success handling, oidcLogout, a logout success handler, and csrf.spa(). Done means the page explains which of these settings a BFF needs for post-login and post-logout redirects. The multi-frontend whitelist idea is a design question and is out of scope.

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

説明

waiting-for-triage

Here, using BFF and token relay patterns becomes popular (see https://datatracker.ietf.org/doc/rfc10017).

TokenRelayFilter works but requires some tweaks for proper redirection after login and logout.

I think this page https://docs.spring.io/spring-cloud-gateway/reference/spring-cloud-gateway-server-webmvc/filters/tokenrelay.html could give more details about the settings required to have a working BFF.

Here is what I have:

@Bean
SecurityFilterChain oauth2SecurityFilterChain(HttpSecurity http, ClientRegistrationRepository registrations) {
	String frontendUrl = "http://localhost:9000";

	var authenticationSuccessHandler = new SimpleUrlAuthenticationSuccessHandler(frontendUrl);
	var logoutSuccessHandler = new OidcClientInitiatedLogoutSuccessHandler(registrations);
	logoutSuccessHandler.setPostLogoutRedirectUri(frontendUrl);

	http.oauth2Login(cust -> cust.successHandler(authenticationSuccessHandler));
	http.oauth2Client(withDefaults());
	http.oidcLogout(oidc -> oidc.backChannel(Customizer.withDefaults()));
	http.logout(cust -> cust.logoutSuccessHandler(logoutSuccessHandler));
	http.csrf(x -> x.spa());

	return http.build();
}

The frontend URL must be a valid redirect URL (OIDC client settings) otherwise redirect after logout fails.

Using

var logoutSuccessHandler =  new SimpleUrlLogoutSuccessHandler();
logoutSuccessHandler.setDefaultTargetUrl(frontendUrl);

is a kind of trap because it invalidates the session at the gateway level but does not call OIDC server so at the OIDC server level the user is still logged.

The frontend url can be declared as a custom property but there will still be is a 1-1 relationship between the BFF and the frontend. It respects the "backend for frontend" semantic ("frontend" is singular) but frontend url could be transmitted in the URL when the client logs in (GET on http://bff/oauth2/authorization/+registrationid) and saved in session for the final redirection. A whitelist of approved frontends could be declared somewhere. Adding a BFF make the infrastructure more complex so if a single BFF can serve several frontends it fosters adoption.

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

環境構築

はじめの一歩

  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 を短くまとめたダイジェスト。