春のセキュリティの性質
スプリングセキュリティは基本的に一連のフィルターであり、フィルターチェーンとしてフィルターチェーンに挿入され、フィルターチェーンプロキシと呼ばれます。図に示されているように。
実際、FilterChainProxyの下に複数のフィルターチェーンがあり、異なるURLを検証することができ、フィルターチェーンに所有されているフィルターは、定義されたサービスに従って自動的に増加または減少します。したがって、独自のロジックを実装したくない限り、これらのフィルターを表示および定義する必要はありません。
重要なカテゴリ
認証
認証は、ユーザー認証情報を表すために使用されるインターフェイスです。ユーザーがログインして認証する前に、関連情報は認証固有の実装クラスのオブジェクトにカプセル化されます。ログイン認証が成功した後、ユーザー許可やその他の情報を含むより包括的な情報を備えた認証オブジェクトが生成され、アクセス権の認証など、後続のプログラムを呼び出すためにSecurityContexTholderが保持しているSecurityContextに保存されます。
AuthenticationManager
検証に使用されるメインインターフェイスはAuthenticationManagerで、1つの方法しかありません。
public interface authenticationmanager {authentication authentication(authentication authentication)slows authenticationexception;}Authentiate()メソッドが実行された後、3つの状況があります。
検証は成功し、ユーザー情報を使用した認証が返されます。
検証に失敗し、認証Exception例外がスローされました。
判断できない、nullを返します。
Providermanager
ProviderManagerは、上記のAuthenticationManagerの最も一般的な実装です。検証自体を処理しませんが、検証を構成するAuthenticationProviderリストに委任し、各AuthenticationProviderを順番に呼び出します。このプロセスでは、AuthenticationProviderが正常に認証されている限り、検証は継続されません。この認証結果は、ProviderManagerの認証結果として直接使用されます。
認定プロセス
ユーザーは、ユーザー名とパスワードでログインします。
Spring Securityは、取得したユーザー名とパスワードを、一般的に使用されるusernamepasswordauthenticationtokenなどの認証インターフェイス実装クラスにカプセル化します。
認証のために、上記の認証オブジェクトをAuthenticationManager Class ProviderManagerに渡します。
ProviderManagerは、認証のために各AuthenticationProviderを順番に呼び出します。認証が成功すると、認証オブジェクトがユーザーの権限をカプセル化し、その他の情報が返されます。
AuthenticationManagerによって返された認証オブジェクトを現在のSecurityContextに割り当てます。
カスタム検証
上記の知識リザーブを使用すると、検証方法をカスタマイズできます。上記から、実際には、AuthenticationProvidersが実際に検証操作を実行するために使用されていることがわかります。したがって、検証方法をカスタマイズする場合は、独自のAuthenticationProviderを実装してからProviderManagerに追加する必要があります。
AuthenticationProviderをカスタマイズします
@componentPublic class CustomAuthenticationProvider Explentice AuthenticationProvider {@Override Public Authentication Authentication(Authentication Authentication)Slows AuthenticationException {string name = exhindication.getName();文字列password = authentication.getcredentials()。toString(); if(shuldauthenticateagainthirdpartySystem()){//資格情報//を使用し、サードパーティシステムに対して認証new usernamepasswordauthenticationtoken(name、password、new arraylist <>()); } else {return null; }} @Override public boolean supports(class <?> authentication){return authentication.equals(usernamepasswordauthenticationtoken.class); }}supports()メソッドは、認証パラメーターを受け入れて、渡された認証が認証プロバイダーが処理できるタイプであるかどうかを判断します。
AuthenticationProviderを登録します
ここで、ProviderManagerで作成したばかりのAuthenticationProviderを登録し、すべての操作が完了します。
@configuration@enablewebsecurity@componentscan( "org.baeldung.security")public class securityconfigは、websecurityconfigureradapter {@autowired private customauthenticationprovider authprovider; @Override Protected void configure(authenticationmanagerbuilder auth)スロー{auth.authenticationprovider(authprovider); } @Override Protected void configure(httpsecurity http)throws exception {http.authorizerequests()。anyRequest()。 }}要約します
上記は、編集者によって紹介されたスプリングセキュリティ検証プロセス分析とカスタム検証方法です。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!