最新の安定バージョンについては、Spring Security 6.3.1 を使用してください!

高度な構成

HttpSecurity.oauth2Login() は、OAuth 2.0 ログインをカスタマイズするための多くの構成オプションを提供します。主な構成オプションは、対応するプロトコルエンドポイントにグループ化されます。

例: oauth2Login().authorizationEndpoint() では認可エンドポイントを構成できますが、oauth2Login().tokenEndpoint() ではトークンエンドポイントを構成できます。

次のコードは例を示しています。

高度な OAuth2 ログイン構成
  • Java

  • Kotlin

@EnableWebSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -> oauth2
			    .authorizationEndpoint(authorization -> authorization
			            ...
			    )
			    .redirectionEndpoint(redirection -> redirection
			            ...
			    )
			    .tokenEndpoint(token -> token
			            ...
			    )
			    .userInfoEndpoint(userInfo -> userInfo
			            ...
			    )
			);
		return http.build();
	}
}
@EnableWebSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            oauth2Login {
                authorizationEndpoint {
                    ...
                }
                redirectionEndpoint {
                    ...
                }
                tokenEndpoint {
                    ...
                }
                userInfoEndpoint {
                    ...
                }
            }
        }
        return http.build()
    }
}

oauth2Login() DSL の主なゴールは、仕様で定義されているように、命名に厳密に整合することでした。

OAuth 2.0 認可フレームワークは、プロトコルエンドポイント [IETF] (英語) を次のように定義します。

認可プロセスでは、2 つの認可サーバーエンドポイント(HTTP リソース)を使用します。

  • 認可エンドポイント: クライアントがユーザーエージェントリダイレクトを介してリソース所有者から認可を取得するために使用します。

  • トークンエンドポイント: 通常はクライアント認証で、アクセストークンの認可付与を交換するためにクライアントによって使用されます。

1 つのクライアントエンドポイントと同様に:

  • リダイレクトエンドポイント: リソース所有者のユーザーエージェントを介してクライアントに認証資格情報を含むレスポンスを返すために認証サーバーによって使用されます。

OpenID Connect Core 1.0 仕様では、UserInfo エンドポイント (英語) を次のように定義しています。

UserInfo エンドポイントは、認証されたエンドユーザーに関するクレームを返す OAuth 2.0 保護リソースです。エンドユーザーに関するリクエストされたクレームを取得するために、クライアントは OpenID Connect 認証を通じて取得されたアクセストークンを使用して UserInfo エンドポイントにリクエストを行います。通常、これらのクレームは、クレームの名前と値のペアのコレクションを含む JSON オブジェクトによって表されます。

次のコードは、oauth2Login() DSL で使用可能な完全な構成オプションを示しています。

OAuth2 ログイン構成オプション
  • Java

  • Kotlin

@EnableWebSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -> oauth2
			    .clientRegistrationRepository(this.clientRegistrationRepository())
			    .authorizedClientRepository(this.authorizedClientRepository())
			    .authorizedClientService(this.authorizedClientService())
			    .loginPage("/login")
			    .authorizationEndpoint(authorization -> authorization
			        .baseUri(this.authorizationRequestBaseUri())
			        .authorizationRequestRepository(this.authorizationRequestRepository())
			        .authorizationRequestResolver(this.authorizationRequestResolver())
			    )
			    .redirectionEndpoint(redirection -> redirection
			        .baseUri(this.authorizationResponseBaseUri())
			    )
			    .tokenEndpoint(token -> token
			        .accessTokenResponseClient(this.accessTokenResponseClient())
			    )
			    .userInfoEndpoint(userInfo -> userInfo
			        .userAuthoritiesMapper(this.userAuthoritiesMapper())
			        .userService(this.oauth2UserService())
			        .oidcUserService(this.oidcUserService())
			    )
			);
		return http.build();
	}
}
@EnableWebSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            oauth2Login {
                clientRegistrationRepository = clientRegistrationRepository()
                authorizedClientRepository = authorizedClientRepository()
                authorizedClientService = authorizedClientService()
                loginPage = "/login"
                authorizationEndpoint {
                    baseUri = authorizationRequestBaseUri()
                    authorizationRequestRepository = authorizationRequestRepository()
                    authorizationRequestResolver = authorizationRequestResolver()
                }
                redirectionEndpoint {
                    baseUri = authorizationResponseBaseUri()
                }
                tokenEndpoint {
                    accessTokenResponseClient = accessTokenResponseClient()
                }
                userInfoEndpoint {
                    userAuthoritiesMapper = userAuthoritiesMapper()
                    userService = oauth2UserService()
                    oidcUserService = oidcUserService()
                }
            }
        }
        return http.build()
    }
}

oauth2Login() DSL に加えて、XML 構成もサポートされています。

次のコードは、セキュリティ名前空間で使用できる完全な構成オプションを示しています。

OAuth2 ログイン XML 設定オプション
<http>
	<oauth2-login client-registration-repository-ref="clientRegistrationRepository"
				  authorized-client-repository-ref="authorizedClientRepository"
				  authorized-client-service-ref="authorizedClientService"
				  authorization-request-repository-ref="authorizationRequestRepository"
				  authorization-request-resolver-ref="authorizationRequestResolver"
				  access-token-response-client-ref="accessTokenResponseClient"
				  user-authorities-mapper-ref="userAuthoritiesMapper"
				  user-service-ref="oauth2UserService"
				  oidc-user-service-ref="oidcUserService"
				  login-processing-url="/login/oauth2/code/*"
				  login-page="/login"
				  authentication-success-handler-ref="authenticationSuccessHandler"
				  authentication-failure-handler-ref="authenticationFailureHandler"
				  jwt-decoder-factory-ref="jwtDecoderFactory"/>
</http>

以下のセクションでは、使用可能な各構成オプションについて詳しく説明します。

OAuth 2.0 ログインページ

デフォルトでは、OAuth 2.0 ログインページは DefaultLoginPageGeneratingFilter によって自動生成されます。デフォルトのログインページには、設定された各 OAuth クライアントとその ClientRegistration.clientName がリンクとして表示され、認可リクエスト(または OAuth 2.0 ログイン)を開始できます。

DefaultLoginPageGeneratingFilter が構成済みの OAuth クライアントのリンクを表示するには、登録された ClientRegistrationRepository が Iterable<ClientRegistration> も実装する必要があります。参考のために InMemoryClientRegistrationRepository を参照してください。

各 OAuth クライアントのリンクの宛先は、デフォルトで次のようになります。

OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI + "/{registrationId}"

次の行に例を示します。

<a href="/oauth2/authorization/google">Google</a>

デフォルトのログインページをオーバーライドするには、oauth2Login().loginPage() および(オプションで) oauth2Login().authorizationEndpoint().baseUri() を構成します。

次のリストに例を示します。

OAuth2 ログインページの構成
  • Java

  • Kotlin

  • XML

@EnableWebSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -> oauth2
			    .loginPage("/login/oauth2")
			    ...
			    .authorizationEndpoint(authorization -> authorization
			        .baseUri("/login/oauth2/authorization")
			        ...
			    )
			);
		return http.build();
	}
}
@EnableWebSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            oauth2Login {
                loginPage = "/login/oauth2"
                authorizationEndpoint {
                    baseUri = "/login/oauth2/authorization"
                }
            }
        }
        return http.build()
    }
}
<http>
	<oauth2-login login-page="/login/oauth2"
				  ...
    />
</http>
カスタムログインページを表示できる @RequestMapping("/login/oauth2") を備えた @Controller を提供する必要があります。

前述のように、oauth2Login().authorizationEndpoint().baseUri() の構成はオプションです。ただし、カスタマイズする場合は、各 OAuth クライアントへのリンクが authorizationEndpoint().baseUri() と一致することを確認してください。

次の行に例を示します。

<a href="/login/oauth2/authorization/google">Google</a>

リダイレクトエンドポイント

リダイレクトエンドポイントは、認可サーバーがリソース所有者ユーザーエージェントを介してクライアントに認可レスポンス(認可資格情報を含む)を返すために使用されます。

OAuth 2.0 Login は認証コード付与を活用します。認証情報は認証コードです。

デフォルトの Authorization Response baseUri (リダイレクトエンドポイント)は /login/oauth2/code/* であり、OAuth2LoginAuthenticationFilter.DEFAULT_FILTER_PROCESSES_URI で定義されています。

Authorization Response baseUri をカスタマイズする場合は、次の例に示すように構成します。

リダイレクトエンドポイントの構成
  • Java

  • Kotlin

  • XML

@EnableWebSecurity
public class OAuth2LoginSecurityConfig {

    @Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -> oauth2
			    .redirectionEndpoint(redirection -> redirection
			        .baseUri("/login/oauth2/callback/*")
			        ...
			    )
			);
		return http.build();
	}
}
@EnableWebSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            oauth2Login {
                redirectionEndpoint {
                    baseUri = "/login/oauth2/callback/*"
                }
            }
        }
        return http.build()
    }
}
<http>
	<oauth2-login login-processing-url="/login/oauth2/callback/*"
				  ...
    />
</http>

また、ClientRegistration.redirectUri がカスタム Authorization Response baseUri と一致することを確認する必要があります。

次のリストに例を示します。

  • Java

  • Kotlin

return CommonOAuth2Provider.GOOGLE.getBuilder("google")
	.clientId("google-client-id")
	.clientSecret("google-client-secret")
	.redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
	.build();
return CommonOAuth2Provider.GOOGLE.getBuilder("google")
    .clientId("google-client-id")
    .clientSecret("google-client-secret")
    .redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
    .build()

UserInfo エンドポイント

UserInfo エンドポイントには、次のサブセクションで説明するように、いくつかの構成オプションが含まれています。

ユーザー権限のマッピング

ユーザーが OAuth 2.0 プロバイダーで正常に認証された後、OAuth2User.getAuthorities() (または OidcUser.getAuthorities())が新しいセットの GrantedAuthority インスタンスにマッピングされ、認証の補完時に OAuth2AuthenticationToken に提供されます。

OAuth2AuthenticationToken.getAuthorities() は、hasRole('USER') や hasRole('ADMIN') などでリクエストを認可するために使用されます。

ユーザー権限をマッピングするときに選択できるオプションがいくつかあります。

GrantedAuthoritiesMapper を使用する

GrantedAuthoritiesMapper の実装を提供し、次の例に示すように構成します。

権限付与マッパー構成
  • Java

  • Kotlin

  • XML

@EnableWebSecurity
public class OAuth2LoginSecurityConfig {

    @Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -> oauth2
			    .userInfoEndpoint(userInfo -> userInfo
			        .userAuthoritiesMapper(this.userAuthoritiesMapper())
			        ...
			    )
			);
		return http.build();
	}

	private GrantedAuthoritiesMapper userAuthoritiesMapper() {
		return (authorities) -> {
			Set<GrantedAuthority> mappedAuthorities = new HashSet<>();

			authorities.forEach(authority -> {
				if (OidcUserAuthority.class.isInstance(authority)) {
					OidcUserAuthority oidcUserAuthority = (OidcUserAuthority)authority;

					OidcIdToken idToken = oidcUserAuthority.getIdToken();
					OidcUserInfo userInfo = oidcUserAuthority.getUserInfo();

					// Map the claims found in idToken and/or userInfo
					// to one or more GrantedAuthority's and add it to mappedAuthorities

				} else if (OAuth2UserAuthority.class.isInstance(authority)) {
					OAuth2UserAuthority oauth2UserAuthority = (OAuth2UserAuthority)authority;

					Map<String, Object> userAttributes = oauth2UserAuthority.getAttributes();

					// Map the attributes found in userAttributes
					// to one or more GrantedAuthority's and add it to mappedAuthorities

				}
			});

			return mappedAuthorities;
		};
	}
}
@EnableWebSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            oauth2Login {
                userInfoEndpoint {
                    userAuthoritiesMapper = userAuthoritiesMapper()
                }
            }
        }
        return http.build()
    }

    private fun userAuthoritiesMapper(): GrantedAuthoritiesMapper = GrantedAuthoritiesMapper { authorities: Collection<GrantedAuthority> ->
        val mappedAuthorities = emptySet<GrantedAuthority>()

        authorities.forEach { authority ->
            if (authority is OidcUserAuthority) {
                val idToken = authority.idToken
                val userInfo = authority.userInfo
                // Map the claims found in idToken and/or userInfo
                // to one or more GrantedAuthority's and add it to mappedAuthorities
            } else if (authority is OAuth2UserAuthority) {
                val userAttributes = authority.attributes
                // Map the attributes found in userAttributes
                // to one or more GrantedAuthority's and add it to mappedAuthorities
            }
        }

        mappedAuthorities
    }
}
<http>
	<oauth2-login user-authorities-mapper-ref="userAuthoritiesMapper"
				  ...
    />
</http>

または、次の例に示すように、GrantedAuthoritiesMapper@Bean を登録して、構成に自動的に適用することもできます。

権限付与マッパー Bean の構成
  • Java

  • Kotlin

@EnableWebSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		http
		    .oauth2Login(withDefaults());
		return http.build();
	}

	@Bean
	public GrantedAuthoritiesMapper userAuthoritiesMapper() {
		...
	}
}
@EnableWebSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            oauth2Login { }
        }
        return http.build()
    }

    @Bean
    fun userAuthoritiesMapper(): GrantedAuthoritiesMapper {
        ...
    }
}

OAuth2UserService を使用した委譲ベースの戦略

この戦略は GrantedAuthoritiesMapper を使用するよりも高度ですが、OAuth2UserRequest および OAuth2User (OAuth 2.0 UserService を使用する場合)または OidcUserRequest および OidcUser (OpenID Connect 1.0 UserService を使用する場合)にアクセスできるため、より柔軟です。

OAuth2UserRequest (および OidcUserRequest)は、関連する OAuth2AccessToken へのアクセスを提供します。これは、委譲者がユーザーのカスタム権限をマップする前に、保護されたリソースから権限情報を取得する必要がある場合に非常に便利です。

次の例は、OpenID Connect 1.0 UserService を使用して、委譲ベースの戦略を実装および構成する方法を示しています。

OAuth2UserService の構成
  • Java

  • Kotlin

  • XML

@EnableWebSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -> oauth2
			    .userInfoEndpoint(userInfo -> userInfo
			        .oidcUserService(this.oidcUserService())
			        ...
			    )
			);
		return http.build();
	}

	private OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() {
		final OidcUserService delegate = new OidcUserService();

		return (userRequest) -> {
			// Delegate to the default implementation for loading a user
			OidcUser oidcUser = delegate.loadUser(userRequest);

			OAuth2AccessToken accessToken = userRequest.getAccessToken();
			Set<GrantedAuthority> mappedAuthorities = new HashSet<>();

			// TODO
			// 1) Fetch the authority information from the protected resource using accessToken
			// 2) Map the authority information to one or more GrantedAuthority's and add it to mappedAuthorities

			// 3) Create a copy of oidcUser but use the mappedAuthorities instead
			oidcUser = new DefaultOidcUser(mappedAuthorities, oidcUser.getIdToken(), oidcUser.getUserInfo());

			return oidcUser;
		};
	}
}
@EnableWebSecurity
class OAuth2LoginSecurityConfig  {

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            oauth2Login {
                userInfoEndpoint {
                    oidcUserService = oidcUserService()
                }
            }
        }
        return http.build()
    }

    @Bean
    fun oidcUserService(): OAuth2UserService<OidcUserRequest, OidcUser> {
        val delegate = OidcUserService()

        return OAuth2UserService { userRequest ->
            // Delegate to the default implementation for loading a user
            var oidcUser = delegate.loadUser(userRequest)

            val accessToken = userRequest.accessToken
            val mappedAuthorities = HashSet<GrantedAuthority>()

            // TODO
            // 1) Fetch the authority information from the protected resource using accessToken
            // 2) Map the authority information to one or more GrantedAuthority's and add it to mappedAuthorities
            // 3) Create a copy of oidcUser but use the mappedAuthorities instead
            oidcUser = DefaultOidcUser(mappedAuthorities, oidcUser.idToken, oidcUser.userInfo)

            oidcUser
        }
    }
}
<http>
	<oauth2-login oidc-user-service-ref="oidcUserService"
				  ...
    />
</http>

OAuth 2.0 UserService

DefaultOAuth2UserService は、標準の OAuth 2.0 プロバイダーをサポートする OAuth2UserService の実装です。

OAuth2UserService は、エンドユーザー(リソース所有者)のユーザー属性を UserInfo エンドポイントから取得し(認可フロー中にクライアントに付与されたアクセストークンを使用して)、AuthenticatedPrincipal を OAuth2User の形式で返します。

DefaultOAuth2UserService は、UserInfo エンドポイントでユーザー属性をリクエストするときに RestOperations を使用します。

UserInfo リクエストの前処理をカスタマイズする必要がある場合は、DefaultOAuth2UserService.setRequestEntityConverter() にカスタム Converter<OAuth2UserRequest, RequestEntity<?>> を提供できます。デフォルトの実装 OAuth2UserRequestEntityConverter は、デフォルトで Authorization ヘッダーに OAuth2AccessToken を設定する UserInfo リクエストの RequestEntity 表現を構築します。

一方、UserInfo レスポンスのリアクティブ処理をカスタマイズする必要がある場合は、DefaultOAuth2UserService.setRestOperations() にカスタム構成の RestOperations を提供する必要があります。デフォルトの RestOperations は次のように構成されています。

RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());

OAuth2ErrorResponseErrorHandler は、OAuth 2.0 エラー(400 間違ったリクエスト)を処理できる ResponseErrorHandler です。OAuth 2.0 Error パラメーターを OAuth2Error に変換するために OAuth2ErrorHttpMessageConverter を使用します。

DefaultOAuth2UserService をカスタマイズする場合でも、OAuth2UserService の独自の実装を提供する場合でも、次の例に示すように構成する必要があります。

  • Java

  • Kotlin

@EnableWebSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -> oauth2
			    .userInfoEndpoint(userInfo -> userInfo
			        .userService(this.oauth2UserService())
			        ...
			    )
			);
		return http.build();
	}

	private OAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService() {
		...
	}
}
@EnableWebSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            oauth2Login {
                userInfoEndpoint {
                    userService = oauth2UserService()
                    // ...
                }
            }
        }
        return http.build()
    }

    private fun oauth2UserService(): OAuth2UserService<OAuth2UserRequest, OAuth2User> {
        // ...
    }
}

OpenID Connect 1.0 UserService

OidcUserService は、OpenID Connect 1.0 プロバイダーをサポートする OAuth2UserService の実装です。

OidcUserService は、UserInfo エンドポイントでユーザー属性をリクエストするときに DefaultOAuth2UserService を活用します。

UserInfo リクエストの前処理および / または UserInfo レスポンスのリアクティブ処理をカスタマイズする必要がある場合、OidcUserService.setOauth2UserService() にカスタム構成の DefaultOAuth2UserService を提供する必要があります。

OidcUserService をカスタマイズするか、OpenID Connect 1.0 プロバイダーの OAuth2UserService の独自の実装を提供するかどうかにかかわらず、次の例に示すように構成する必要があります。

  • Java

  • Kotlin

@EnableWebSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -> oauth2
				.userInfoEndpoint(userInfo -> userInfo
				    .oidcUserService(this.oidcUserService())
				    ...
			    )
			);
		return http.build();
	}

	private OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() {
		...
	}
}
@EnableWebSecurity
class OAuth2LoginSecurityConfig {

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            oauth2Login {
                userInfoEndpoint {
                    oidcUserService = oidcUserService()
                    // ...
                }
            }
        }
        return http.build()
    }

    private fun oidcUserService(): OAuth2UserService<OidcUserRequest, OidcUser> {
        // ...
    }
}

ID トークン署名検証

OpenID Connect 1.0 認証は ID トークン (英語) を導入します。ID トークン (英語) は、クライアントが使用する場合の認可サーバーによるエンドユーザーの認証に関するクレームを含むセキュリティトークンです。

ID トークンは JSON Web トークン [IETF] (英語) (JWT)として表され、JSON Web 署名 [IETF] (英語) (JWS)を使用して署名する必要があります。

OidcIdTokenDecoderFactory は、OidcIdToken 署名検証に使用される JwtDecoder を提供します。デフォルトのアルゴリズムは RS256 ですが、クライアントの登録時に割り当てられる場合は異なる場合があります。これらの場合、特定のクライアントに割り当てられた予想される JWS アルゴリズムを返すようにリゾルバーを構成できます。

JWS アルゴリズムリゾルバーは、ClientRegistration を受け入れ、クライアントに期待される JwsAlgorithm を返す Function です。SignatureAlgorithm.RS256 または MacAlgorithm.HS256

次のコードは、すべての ClientRegistration に対してデフォルトで MacAlgorithm.HS256 になるように OidcIdTokenDecoderFactory@Bean を構成する方法を示しています。

  • Java

  • Kotlin

@Bean
public JwtDecoderFactory<ClientRegistration> idTokenDecoderFactory() {
	OidcIdTokenDecoderFactory idTokenDecoderFactory = new OidcIdTokenDecoderFactory();
	idTokenDecoderFactory.setJwsAlgorithmResolver(clientRegistration -> MacAlgorithm.HS256);
	return idTokenDecoderFactory;
}
@Bean
fun idTokenDecoderFactory(): JwtDecoderFactory<ClientRegistration?> {
    val idTokenDecoderFactory = OidcIdTokenDecoderFactory()
    idTokenDecoderFactory.setJwsAlgorithmResolver { MacAlgorithm.HS256 }
    return idTokenDecoderFactory
}
HS256HS384 または HS512 などの MAC ベースのアルゴリズムの場合、client-id に対応する client-secret が署名検証の対称キーとして使用されます。
OpenID Connect 1.0 認証用に複数の ClientRegistration が構成されている場合、JWS アルゴリズムリゾルバーは提供された ClientRegistration を評価して、返すアルゴリズムを決定します。

OpenID Connect 1.0 ログアウト

OpenID Connect セッション管理 1.0 では、クライアントを使用してプロバイダーのエンドユーザーをログアウトできます。利用可能な戦略の 1 つは RP からのログアウト (英語) です。

OpenID プロバイダーがセッション管理とディスカバリ (英語) の両方をサポートしている場合、クライアントは OpenID プロバイダーのディスカバリメタデータ (英語) から end_session_endpointURL を取得できます。これは、次の例のように、issuer-uri を使用して ClientRegistration を構成することで実現できます。

spring:
  security:
    oauth2:
      client:
        registration:
          okta:
            client-id: okta-client-id
            client-secret: okta-client-secret
            ...
        provider:
          okta:
            issuer-uri: https://dev-1234.oktapreview.com

…および RP 開始ログアウトを実装する OidcClientInitiatedLogoutSuccessHandler は、次のように構成できます。

  • Java

  • Kotlin

@EnableWebSecurity
public class OAuth2LoginSecurityConfig {

	@Autowired
	private ClientRegistrationRepository clientRegistrationRepository;

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		http
			.authorizeHttpRequests(authorize -> authorize
				.anyRequest().authenticated()
			)
			.oauth2Login(withDefaults())
			.logout(logout -> logout
				.logoutSuccessHandler(oidcLogoutSuccessHandler())
			);
		return http.build();
	}

	private LogoutSuccessHandler oidcLogoutSuccessHandler() {
		OidcClientInitiatedLogoutSuccessHandler oidcLogoutSuccessHandler =
				new OidcClientInitiatedLogoutSuccessHandler(this.clientRegistrationRepository);

		// Sets the location that the End-User's User Agent will be redirected to
		// after the logout has been performed at the Provider
		oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}");

		return oidcLogoutSuccessHandler;
	}
}
@EnableWebSecurity
class OAuth2LoginSecurityConfig {
    @Autowired
    private lateinit var clientRegistrationRepository: ClientRegistrationRepository

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            authorizeRequests {
                authorize(anyRequest, authenticated)
            }
            oauth2Login { }
            logout {
                logoutSuccessHandler = oidcLogoutSuccessHandler()
            }
        }
        return http.build()
    }

    private fun oidcLogoutSuccessHandler(): LogoutSuccessHandler {
        val oidcLogoutSuccessHandler = OidcClientInitiatedLogoutSuccessHandler(clientRegistrationRepository)

        // Sets the location that the End-User's User Agent will be redirected to
        // after the logout has been performed at the Provider
        oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}")
        return oidcLogoutSuccessHandler
    }
}
OidcClientInitiatedLogoutSuccessHandler は、{baseUrl} プレースホルダーをサポートします。使用する場合、app.example.org (英語) などのアプリケーションのベース URL は、リクエスト時に置き換えられます。