OAuth 2.0 移行
JwtTypeValidator で typ ヘッダーを検証する
6.5 の準備手順で validateTypes を false に設定した場合は、これを削除できます。また、JwtTypeValidator をデフォルトリストに明示的に追加することでも削除できます。
例: これを変更します:
Java
Kotlin
@Bean
JwtDecoder jwtDecoder() {
NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
.validateTypes(false) (1)
// ... your remaining configuration
.build();
jwtDecoder.setJwtValidator(JwtValidators.createDefaultWithValidators(
new JwtIssuerValidator(location), JwtTypeValidator.jwt())); (2)
return jwtDecoder;
}
@Bean
fun jwtDecoder(): JwtDecoder {
val jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
.validateTypes(false) (1)
// ... your remaining configuration
.build()
jwtDecoder.setJwtValidator(JwtValidators.createDefaultWithValidators(
JwtIssuerValidator(location), JwtTypeValidator.jwt())) (2)
return jwtDecoder
}
| 1 | - Nimbus をオフにして typ を確認します |
| 2 | - デフォルトの typ バリデーターを追加する |
これに:
Java
Kotlin
@Bean
JwtDecoder jwtDecoder() {
NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
// ... your remaining configuration (1)
.build();
jwtDecoder.setJwtValidator(JwtValidators.createDefaultWithIssuer(location)); (2)
return jwtDecoder;
}
@Bean
fun jwtDecoder(): JwtDecoder {
val jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
// ... your remaining configuration
.build()
jwtDecoder.setJwtValidator(JwtValidators.createDefaultWithIssuer(location)) (2)
return jwtDecoder
}
| 1 | - validateTypes はデフォルトで false になります |
| 2 | - JwtTypeValidator#jwt はすべての createDefaultXXX メソッドで追加されます |
AuthenticationConverter から BearerTokenAuthenticationFilter を提供する
Spring Security 7 では、BearerTokenAuthenticationFilter#setBearerTokenResolver と #setAuthenticaionDetailsSource は非推奨となり、代わりに BearerTokenAuthenticationConverter でこれらを構成するようになりました。
oauth2ResourceServer DSL はほとんどの使用ケースに対応しており、何もする必要はありません。
BearerTokenResolver または AuthenticationDetailsSource を BearerTokenAuthenticationFilter に直接設定する場合は、次のようになります。
Java
Kotlin
BearerTokenAuthenticationFilter filter = new BearerTokenAuthenticationFilter(authenticationManager);
filter.setBearerTokenResolver(myBearerTokenResolver);
filter.setAuthenticationDetailsSource(myAuthenticationDetailsSource);
val filter = BearerTokenAuthenticationFilter(authenticationManager)
filter.setBearerTokenResolver(myBearerTokenResolver)
filter.setAuthenticationDetailsSource(myAuthenticationDetailsSource)
両方を指定するには、BearerTokenAuthenticationConverter を使用することをお勧めします。
- Java
BearerTokenAuthenticationConverter authenticationConverter = new BearerTokenAuthenticationConverter(); authenticationConverter.setBearerTokenResolver(myBearerTokenResolver); authenticationConverter.setAuthenticationDetailsSource(myAuthenticationDetailsSource); BearerTokenAuthenticationFilter filter = new BearerTokenAuthenticationFilter(authenticationManager, authenicationConverter);- Kotlin
val authenticationConverter = BearerTokenAuthenticationConverter() authenticationConverter.setBearerTokenResolver(myBearerTokenResolver) authenticationConverter.setAuthenticationDetailsSource(myAuthenticationDetailsSource) val filter = BearerTokenAuthenticationFilter(authenticationManager, authenticationConverter)
== ` NimbusJwtDecoder のデフォルトの接続タイムアウトと読み取りタイムアウトが 30 秒になりました
NimbusJwtDecoder’s default `RestOperations は、RestOperations が設定されていない場合に JWK セットを取得するために使用されますが、以前は 500 ミリ秒の接続および読み取りタイムアウトを使用していました。この値は多くの デプロイにとって短すぎたため、現在は 30 秒に変更され、ClientRegistrations など、OAuth2 クライアントおよびリソースサーバーのサポートの他の場所ですでに使用されている接続および読み取りタイムアウトと一致するようになりました。
アプリケーションが以前の短いタイムアウトに依存している場合(たとえば、認証サーバーにアクセスできない場合にすぐに失敗することを期待している場合)、JDK の sun.net.client.defaultConnectTimeout および sun.net.client.defaultReadTimeout システムプロパティ (ミリ秒単位) を設定するか、タイムアウトの構成に従って独自の RestOperations を提供することで、それを復元できます。