最新の安定バージョンについては、Spring Security 7.0.2 を使用してください! |
Spring Security 6.4 の新機能
Spring Security 6.4 には、いくつかの新機能があります。以下はリリースのハイライトです。また、各機能とバグ修正の詳細なリストについては、リリースノート [GitHub] (英語) を参照してください。
廃止通知
Spring Security 7 が近づくにつれて、廃止予定について最新情報を把握しておくことが重要になります。そのため、このセクションでは、6.4 リリースでの廃止予定について説明します。
メソッドのセキュリティ -
AuthorizationManager#checkは非推奨となり、代わりにAuthorizationManager#authorizeが推奨されます。これは主に、戻り値の型を具象クラスではなくインターフェースにできるようにするためです。AuthorizationManager#checkを呼び出す場合は、代わりにAuthorizationManager#authorizeを呼び出してください。関連して、
AuthorizationDecisionを受け取るAuthorizationEventPublisher#publishEventは非推奨となり、代わりにAuthorizationResultインターフェースを受け取る同じ名前のメソッドが推奨されます。メソッドのセキュリティ -
PrePostTemplateDefaultsは非推奨となり、より汎用的なAnnotationTemplateExpressionDefaultsが推奨されます。これは、@AuthenticationPrincipalと@CurrentSecurityContextのメタアノテーションプロパティもサポートされるようになったためです。PrePostTemplateDefaultsを構築する場合は、これをAnnotationTemplateExpressionDefaultsに変更してください。OAuth 2.0 -
NimbusOpaqueTokenIntrospectorは、Spring Security OAuth 2.0 リソースサーバーのoidc-oauth2-sdkパッケージへの依存を排除するために、SpringOpaqueTokenIntrospectorに置き換えられて非推奨になりました。NimbusOpaqueTokenIntrospectorを構築する場合は、SpringOpaqueTokenIntrospectorのコンストラクターに置き換えてください。OAuth 2.0 -
DefaultAuthorizationCodeTokenResponseClient、DefaultClientCredentialsTokenResponseClient、DefaultJwtBearerTokenResponseClient、DefaultPasswordTokenResponseClient、DefaultRefreshTokenTokenResponseClient、DefaultTokenExchangeTokenResponseClientは非推奨となり、代わりにRestClientが推奨されます。関連して、
JwtBearerGrantRequestEntityConverter、OAuth2AuthorizationCodeGrantRequestEntityConverter、OAuth2ClientCredentialsGrantRequestEntityConverter、OAuth2PasswordGrantRequestEntityConverter、OAuth2RefreshTokenGrantRequestEntityConverterは非推奨となり、代わりに上記のトークンレスポンスクライアントの 1 つにDefaultOAuth2TokenRequestParametersConverterのインスタンスを提供するようになりました。例: 次のような配置の場合:
private static class MyCustomConverter extends AbstractOAuth2AuthorizationGrantRequestEntityConverter<OAuth2AuthorizationCodeGrantRequest> { @Override protected MultiValueMap<String, String> createParameters (OAuth2AuthorizationCodeGrantRequest request) { MultiValueMap<String, String> parameters = super.createParameters(request); parameters.add("custom", "value"); return parameters; } } @Bean OAuth2AccessTokenResponseClient authorizationCode() { DefaultAuthorizationCodeTokenResponseClient client = new DefaultAuthorizationCodeTokenResponseClient(); Converter<AuthorizationCodeGrantRequest, RequestEntity<?>> entityConverter = new OAuth2AuthorizationCodeGrantRequestEntityConverter(); entityConverter.setParametersConverter(new MyCustomConverter()); client.setRequestEntityConverter(entityConverter); return client; }この構成は
DefaultAuthorizationCodeTokenResponseClientとOAuth2AuthorizationCodeGrantRequestEntityConverterを使用するため非推奨です。現在推奨される構成は次のとおりです。private static class MyCustomConverter implements Converter<OAuth2AuthorizationCodeGrantRequest, Map<String, String>> { @Override public MultiValueMap<String, String> convert(OAuth2AuthorizeCodeGrantRequest request) { MultiValueMap<String, String> parameters = OAuth2AuthorizationCodeGrantRequest.defaultParameters(request); parameters.add("custom", "value"); return parameters; } } @Bean OAuth2AccessTokenResponseClient authorizationCode() { RestClientAuthorizationCodeTokenResponseClient client = new RestClientAuthorizationCodeTokenResponseClient(); client.setParametersConverter(new MyCustomConverter()); return client; }SAML 2.0 - Spring Security SAML 2.0 サービスプロバイダーのインターフェースのバージョンなしの OpenSAML 実装は、バージョン付きの実装に置き換えられ、非推奨になりました。例:
OpenSamlAuthenticationTokenConverterは、現在OpenSaml4AuthenticationTokenConverterとOpenSaml5AuthenticationTokenConverterに置き換えられています。これらの非推奨バージョンのいずれかを構築する場合は、使用している OpenSAML バージョンに対応するバージョンに置き換えてください。SAML 2.0 -
AssertingPartyDetailsに関連するメソッドは非推奨となり、代わりにAssertingPartyMetadataインターフェースを使用する同等のメソッドが推奨されます。LDAP -
DistinguishedNameの使用は、Spring LDAP の廃止に合わせて非推奨となりました。
ワンタイムトークンログイン
Spring Security は、JDBC サポートを含む
oneTimeTokenLogin()DSL 経由のワンタイムトークンログインをサポートするようになりました。
パスキー
Spring Security にパスキーのサポートが追加されました。
メソッドのセキュリティ
すべてのメソッドセキュリティアノテーションが フレームワークの
@AliasFor(Javadoc) をサポートするようになりました@AuthenticationPrincipalおよび@CurrentSecurityContextはアノテーションテンプレートをサポートするようになりました。つまり、次のように Spring のメタアノテーションサポートを使用できるようになります。
Java
Kotlin
@Target(TargetType.TYPE) @Retention(RetentionPolicy.RUNTIME) @AuthenticationPrincipal("claims['{claim}']") @interface CurrentUsername { String claim() default "sub"; } // ... @GetMapping public String method(@CurrentUsername("username") String username) { // ... }annotation CurrentUsername(val claim: String = "sub") // ... @GetMapping fun method(@CurrentUsername("username") val username: String): String { // ... }いくつか [GitHub] (英語) の の改善 [GitHub] (英語) により、セキュリティのアノテーション検索が
AbstractFallbackMethodSecurityMetadataSourceのアルゴリズムと一致するようになりました [GitHub] (英語) 。これにより、以前のバージョンの Spring Security からの移行が容易になります。ネイティブアプリケーションで
@AuthorizeReturnObjectが使用できるようになりましたネイティブアプリケーションは
@PreAuthorizeおよび@PostAuthorizeの Bean を参照できるようになりましたSecurityAnnotationScannersは、セキュリティアノテーションをスキャンし、カスタムアノテーションにセキュリティの選択機能とテンプレート機能を追加するための便利な API [GitHub] (英語) を提供します。
OAuth 2.0
oauth2Login()がOAuth2AuthorizationRequestResolverを@Beanとして [GitHub] (英語) を受け入れるようになりましたClientRegistrationsは外部から取得した構成をサポートするようになりましたリアクティブ
oauth2Login()の DSL にloginPage()を追加OIDC バックチャネルサポートで、
logout+jwt型のログアウトトークン [GitHub] (英語) が受け入れられるようになりました。RestClientはOAuth2ClientHttpRequestInterceptorと組み合わせて 保護されたリソースのリクエストを行うことができるようになりました。アクセストークンリクエストのより一貫した構成のために、
OAuth2AccessTokenResponseClientのRestClientベースの実装を追加しました。RestClientサポートの使用をオプトインするには、次の例のように、各権限付与型に対して Bean を公開するだけです。Java
Kotlin
@Configuration public class SecurityConfig { @Bean public OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> authorizationCodeAccessTokenResponseClient() { return new RestClientAuthorizationCodeTokenResponseClient(); } @Bean public OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> refreshTokenAccessTokenResponseClient() { return new RestClientRefreshTokenTokenResponseClient(); } @Bean public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsAccessTokenResponseClient() { return new RestClientClientCredentialsTokenResponseClient(); } @Bean public OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> jwtBearerAccessTokenResponseClient() { return new RestClientJwtBearerTokenResponseClient(); } @Bean public OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> tokenExchangeAccessTokenResponseClient() { return new RestClientTokenExchangeTokenResponseClient(); } }@Configuration class SecurityConfig { @Bean fun authorizationCodeAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> { return RestClientAuthorizationCodeTokenResponseClient() } @Bean fun refreshTokenAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> { return RestClientRefreshTokenTokenResponseClient() } @Bean fun clientCredentialsAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> { return RestClientClientCredentialsTokenResponseClient() } @Bean fun jwtBearerAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> { return RestClientJwtBearerTokenResponseClient() } @Bean fun tokenExchangeAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> { return RestClientTokenExchangeTokenResponseClient() } }トークン交換はリフレッシュトークンをサポートするようになり [GitHub] (英語) ました
SAML 2.0
OpenSAML 5 サポートを追加しました。これで、OpenSAML 4 または OpenSAML 5 のいずれかを使用できます。デフォルトでは、Spring Security はクラスパスに基づいて適切な実装を選択します。
registrationIdでの EntityID の使用が簡素化されます。一般的なパターンは、
entityIDによってアサーションパーティを識別することです。以前のバージョンでは、これにはOpenSamlAuthenticationRequestResolverを直接構成する必要がありました。現在、リクエストリゾルバーは、パス内での検索に加えて、リクエストパラメーターとして [GitHub] (英語)registrationIdをデフォルトで検索します。これにより、registrationId値を変更したり、リクエストリゾルバーをカスタマイズしたりすることなく、RelyingPartyRegistrationsまたはOpenSaml4/5AssertingPartyMetadataRepositoryを使用できます。関連して、
authenticationRequestUriにクエリパラメーターを含めるように設定できるようになりました。メタデータの有効期限に応じて、アサーティングパーティをバックグラウンドでリフレッシュできるようになりました。
例:
OpenSaml5AssertingPartyMetadataRepositoryを使用して次の操作を実行できるようになりました。Java
Kotlin
@Component public class RefreshableRelyingPartyRegistrationRepository implements IterableRelyingPartyRegistrationRepository { private final AssertingPartyMetadataRepository assertingParties = OpenSaml5AssertingPartyMetadataRepository .fromTrustedMetadataLocation("https://idp.example.org").build(); @Override public RelyingPartyRegistration findByRegistrationId(String registrationId) { AssertingPartyMetadata assertingParty = this.assertingParties.findByEntityId(registrationId); return RelyingPartyRegistration.withAssertingPartyMetadata(assertingParty) // relying party configurations .build(); } // ... }@Component open class RefreshableRelyingPartyRegistrationRepository: IterableRelyingPartyRegistrationRepository { private val assertingParties: AssertingPartyMetadataRepository = OpenSaml5AssertingPartyMetadataRepository .fromTrustedMetadataLocation("https://idp.example.org").build() override fun findByRegistrationId(String registrationId): RelyingPartyRegistration { val assertingParty = this.assertingParties.findByEntityId(registrationId) return RelyingPartyRegistration.withAssertingPartyMetadata(assertingParty) // relying party configurations .build() } // ... }この実装では、メタデータの署名の検証もサポートされています。
証明書利用者メタデータ [GitHub] (英語) に署名できるようになりました
RelyingPartyRegistrationRepositoryの結果をキャッシュ (Javadoc) できるようになりました。これは、登録値の読み込みをアプリケーションの起動後まで延期する場合に役立ちます。また、Spring キャッシュを介してメタデータがリフレッシュされるタイミングを制御する場合にも役立ちます。SAML 2.0 標準に準拠するため、メタデータエンドポイントは
application/samlmetadata+xmlMIME 型を使用するようになりました [GitHub] (英語) 。
Web
CSRF BREACH トークンの一貫性が向上しまし [GitHub] (英語) た
「自分を記憶」クッキーはよりカスタマイズ可能 [GitHub] (英語) になりました
セキュリティフィルターチェーンは、さらに無効な構成を検出します。例: 任意リクエストフィルターチェーンの後に宣言されたフィルターチェーンは、呼び出されないため無効です。
Java
Kotlin
@Bean @Order(0) SecurityFilterChain api(HttpSecurity http) throws Exception { http // implicit securityMatcher("/**") .authorizeHttpRequests(...) .httpBasic(...) return http.build(); } @Bean @Order(1) SecurityFilterChain app(HttpSecurity http) throws Exception { http .securityMatcher("/app/**") .authorizeHttpRequests(...) .formLogin(...) return http.build(); }@Bean @Order(0) fun api(val http: HttpSecurity): SecurityFilterChain { http { authorizeHttpRequests { // ... } } return http.build() } @Bean @Order(1) fun app(val http: HttpSecurity): SecurityFilterChain { http { securityMatcher("/app/**") authorizeHttpRequests { // ... } } return http.build() }詳細については、関連チケット [GitHub] (英語) を参照してください。
ServerHttpSecurityはServerWebExchangeFirewallを@Beanとして取得します [GitHub] (英語)
可観測性
Observability は現在認可、認証、リクエストの監視を個別に切り替えるをサポートしています。例: チェーン観測のフィルターをオフにするには、次のような @Bean を公開できます。
Java
Kotlin
@Bean
SecurityObservationSettings allSpringSecurityObservations() {
return SecurityObservationSettings.withDefaults()
.shouldObserveFilterChains(false).build();
}
@Bean
fun allSpringSecurityObservations(): SecurityObservationSettings {
return SecurityObservationSettings.builder()
.shouldObserveFilterChains(false).build()
}
Kotlin
Kotlin DSL は SAML 2.0 [GitHub] (英語) 、
GrantedAuthorityDefaults[GitHub] (英語) 、RoleHierarchy[GitHub] (英語) 、@Beanをサポートするようになりました。@PreFilterと@PostFilterが Kotlin でサポートされるようになりました [GitHub] (英語)Kotlin リアクティブ DSL が
SecurityContextRepository[GitHub] (英語) をサポートするようになりました
ACL
AclAuthorizationStrategyImplがRoleHierarchyをサポートするようになり [GitHub] (英語) ました