クラス AbstractAuthenticationProcessingFilter

java.lang.ObjectSE
org.springframework.web.filter.GenericFilterBean
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter
実装されたすべてのインターフェース:
jakarta.servlet.Filterorg.springframework.beans.factory.Awareorg.springframework.beans.factory.BeanNameAwareorg.springframework.beans.factory.DisposableBeanorg.springframework.beans.factory.InitializingBeanorg.springframework.context.ApplicationEventPublisherAwareorg.springframework.context.EnvironmentAwareorg.springframework.context.MessageSourceAwareorg.springframework.core.env.EnvironmentCapableorg.springframework.web.context.ServletContextAware
既知の直属サブクラス
CasAuthenticationFilterOAuth2LoginAuthenticationFilterSaml2WebSsoAuthenticationFilterUsernamePasswordAuthenticationFilter

public abstract class AbstractAuthenticationProcessingFilter extends org.springframework.web.filter.GenericFilterBean implements org.springframework.context.ApplicationEventPublisherAware, org.springframework.context.MessageSourceAware
ブラウザーベースの HTTP ベースの認証リクエストの抽象プロセッサー。

認証プロセス

フィルターを使用するには、 authenticationManager プロパティを設定する必要があります。クラスの実装によって作成された認証リクエストトークンを処理するには、 AuthenticationManager が必要です。

このフィルターは、リクエストをインターセプトし、リクエストが setRequiresAuthenticationRequestMatcher(RequestMatcher) と一致する場合、そのリクエストから認証を実行しようとします。

認証は、attemptAuthentication メソッドによって実行されます。これは、サブクラスによって実装する必要があります。

Authentication Success

認証が成功した場合、結果の Authentication オブジェクトは現在のスレッドの SecurityContext に配置されます。これは、以前のフィルターによってすでに作成されていることが保証されています。

その後、構成された AuthenticationSuccessHandler が呼び出され、ログインが成功した後、適切な宛先にリダイレクトされます。デフォルトの動作は SavedRequestAwareAuthenticationSuccessHandler に実装されており、 ExceptionTranslationFilter によって設定されたすべての DefaultSavedRequest を利用し、そこに含まれる URL にユーザーをリダイレクトします。それ以外の場合は、webapp ルート "/" にリダイレクトされます。この動作をカスタマイズするには、このクラスの異なる構成のインスタンスを挿入するか、別の実装を使用します。

詳細については、successfulAuthentication(HttpServletRequest, HttpServletResponse, FilterChain, Authentication) メソッドを参照してください。

Authentication Failure

認証が失敗した場合、構成された AuthenticationFailureHandler に委譲して、失敗情報をクライアントに伝達できるようにします。デフォルトの実装は SimpleUrlAuthenticationFailureHandler で、クライアントに 401 エラーコードを送信します。代替として、失敗 URL で構成することもできます。ここでも、必要な動作をここに挿入できます。

Event Publication

認証が成功すると、InteractiveAuthenticationSuccessEvent がアプリケーションコンテキストを介して公開されます。認証が失敗した場合、イベントは公開されません。これは通常、AuthenticationManager -specific アプリケーションイベントを介して記録されるためです。

Session Authentication

クラスにはオプションの SessionAuthenticationStrategy があり、attemptAuthentication() の呼び出しが成功した直後に呼び出されます。異なる実装 can be injected は、セッション固定攻撃の防止などを可能にするか、プリンシパルが同時に実行できるセッションの数を制御します。
  • フィールドの詳細

    • eventPublisher

      protected org.springframework.context.ApplicationEventPublisher eventPublisher
    • authenticationDetailsSource

      protected AuthenticationDetailsSource<jakarta.servlet.http.HttpServletRequest,?> authenticationDetailsSource
    • messages

      protected org.springframework.context.support.MessageSourceAccessor messages
  • コンストラクターの詳細

    • AbstractAuthenticationProcessingFilter

      protected AbstractAuthenticationProcessingFilter(StringSE defaultFilterProcessesUrl)
      パラメーター:
      defaultFilterProcessesUrl - filterProcessesUrl のデフォルト値。
    • AbstractAuthenticationProcessingFilter

      protected AbstractAuthenticationProcessingFilter(RequestMatcher requiresAuthenticationRequestMatcher)
      新しいインスタンスを作成します
      パラメーター:
      requiresAuthenticationRequestMatcher - 認証が必要かどうかを判断するために使用される RequestMatcher null にすることはできません。
    • AbstractAuthenticationProcessingFilter

      protected AbstractAuthenticationProcessingFilter(StringSE defaultFilterProcessesUrl, AuthenticationManager authenticationManager)
      デフォルトの filterProcessesUrl と AuthenticationManager で新しいインスタンスを作成します
      パラメーター:
      defaultFilterProcessesUrl - filterProcessesUrl のデフォルト値。
      authenticationManager - Authentication オブジェクトの認証に使用される AuthenticationManager null にすることはできません。
    • AbstractAuthenticationProcessingFilter

      protected AbstractAuthenticationProcessingFilter(RequestMatcher requiresAuthenticationRequestMatcher, AuthenticationManager authenticationManager)
      RequestMatcherAuthenticationManager で新しいインスタンスを作成します
      パラメーター:
      requiresAuthenticationRequestMatcher - 認証が必要かどうかを判断するために使用される RequestMatcher null にすることはできません。
      authenticationManager - Authentication オブジェクトの認証に使用される AuthenticationManager null にすることはできません。
  • メソッドの詳細

    • afterPropertiesSet

      public void afterPropertiesSet()
      次で指定:
      インターフェース org.springframework.beans.factory.InitializingBeanafterPropertiesSet 
      オーバーライド:
      クラス org.springframework.web.filter.GenericFilterBeanafterPropertiesSet 
    • doFilter

      public void doFilter(jakarta.servlet.ServletRequest request, jakarta.servlet.ServletResponse response, jakarta.servlet.FilterChain chain) throws IOExceptionSE, jakarta.servlet.ServletException
      requiresAuthentication メソッドを呼び出して、リクエストが認証用であり、このフィルターで処理する必要があるかどうかを判別します。認証リクエストの場合は、attemptAuthentication が呼び出されて認証が実行されます。次に、3 つの結果が考えられます。
      1. Authentication オブジェクトが返されます。構成された SessionAuthenticationStrategy が呼び出され(セッション固定攻撃から保護するために新しいセッションを作成するなど、セッション関連の動作を処理するため)、続いて successfulAuthentication(HttpServletRequest, HttpServletResponse, FilterChain, Authentication) メソッドが呼び出されます。
      2. 認証中に AuthenticationException が発生します。unsuccessfulAuthentication メソッドが呼び出されます
      3. Null が返され、認証プロセスが不完全であることを示します。その後、サブクラスが認証プロセスを続行するために必要な作業(リダイレクトなど)を実行したと仮定すると、メソッドはすぐに戻ります。このメソッドは、返された Authentication オブジェクトが null でない場合、以降のリクエストを受信することを前提としています。
      次で指定:
      インターフェース jakarta.servlet.FilterdoFilter 
      例外:
      IOExceptionSE
      jakarta.servlet.ServletException
    • requiresAuthentication

      protected boolean requiresAuthentication(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
      このフィルターが現在の呼び出しのログインリクエストを処理しようとするかどうかを示します。

      filterProcessesUrl プロパティと照合する前に、リクエスト URL の「パス」セクションからパラメーター(https://host/myapp/index.html;jsessionid=blah の jsessionid パラメーターなど)を取り除きます。

      サブクラスは、Tapestry 統合などの特別な要件のためにオーバーライドできます。

      戻り値:
      フィルターが認証を試行する必要がある場合は true、そうでない場合は false
    • attemptAuthentication

      public abstract Authentication attemptAuthentication(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws AuthenticationException, IOExceptionSE, jakarta.servlet.ServletException
      実際の認証を実行します。

      実装では、次のいずれかを実行する必要があります。

      1. 認証されたユーザーの移入された認証トークンを返し、認証の成功を示します
      2. 認証プロセスがまだ進行中であることを示す null を返します。戻る前に、実装はプロセスを完了するために必要な追加作業を実行する必要があります。
      3. 認証プロセスが失敗した場合、 AuthenticationException をスローします
      パラメーター:
      request - パラメーターの抽出元および認証の実行元
      response - レスポンス。これは、実装が多段階認証プロセス (OIDC など) の一部としてリダイレクトを行う必要がある場合に必要になる場合があります。
      戻り値:
      認証されたユーザートークン。認証が不完全な場合は null
      例外:
      AuthenticationException - 認証が失敗した場合。
      IOExceptionSE
      jakarta.servlet.ServletException
    • successfulAuthentication

      protected void successfulAuthentication(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain, Authentication authResult) throws IOExceptionSE, jakarta.servlet.ServletException
      認証成功のデフォルトの動作。
      1. SecurityContextHolder で成功した Authentication オブジェクトを設定します
      2. ログインの成功を構成済みの RememberMeServices に通知します
      3. 構成された ApplicationEventPublisher を介して InteractiveAuthenticationSuccessEvent を起動します
      4. 追加の動作を AuthenticationSuccessHandler に委譲します。
      認証が成功した後、サブクラスはこのメソッドをオーバーライドして FilterChain を続行できます。
      パラメーター:
      request -
      response -
      chain -
      authResult - attemptAuthentication メソッドから返されたオブジェクト。
      例外:
      IOExceptionSE
      jakarta.servlet.ServletException
    • unsuccessfulAuthentication

      protected void unsuccessfulAuthentication(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, AuthenticationException failed) throws IOExceptionSE, jakarta.servlet.ServletException
      失敗した認証のデフォルトの動作。
      1. SecurityContextHolder をクリアします
      2. セッションに例外を格納します (存在する場合、または allowSesssionCreation true に設定されている場合)
      3. ログインの失敗を構成済みの RememberMeServices に通知します
      4. 追加の動作を AuthenticationFailureHandler に委譲します。
      例外:
      IOExceptionSE
      jakarta.servlet.ServletException
    • getAuthenticationManager

      protected AuthenticationManager getAuthenticationManager()
    • setAuthenticationManager

      public void setAuthenticationManager(AuthenticationManager authenticationManager)
    • setFilterProcessesUrl

      public void setFilterProcessesUrl(StringSE filterProcessesUrl)
      認証が必要かどうかを決定する URL を設定します
      パラメーター:
      filterProcessesUrl -
    • setRequiresAuthenticationRequestMatcher

      public final void setRequiresAuthenticationRequestMatcher(RequestMatcher requestMatcher)
    • getRememberMeServices

      public RememberMeServices getRememberMeServices()
    • setRememberMeServices

      public void setRememberMeServices(RememberMeServices rememberMeServices)
    • setContinueChainBeforeSuccessfulAuthentication

      public void setContinueChainBeforeSuccessfulAuthentication(boolean continueChainBeforeSuccessfulAuthentication)
      フィルターチェーンを successfulAuthentication(HttpServletRequest, HttpServletResponse, FilterChain, Authentication) に委譲する前に継続する必要があるかどうかを示します。これは、特定の環境(Tapestry アプリケーションなど)で役立つ場合があります。デフォルトは false です。
    • setApplicationEventPublisher

      public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher eventPublisher)
      次で指定:
      インターフェース org.springframework.context.ApplicationEventPublisherAwaresetApplicationEventPublisher 
    • setAuthenticationDetailsSource

      public void setAuthenticationDetailsSource(AuthenticationDetailsSource<jakarta.servlet.http.HttpServletRequest,?> authenticationDetailsSource)
    • setMessageSource

      public void setMessageSource(org.springframework.context.MessageSource messageSource)
      次で指定:
      インターフェース org.springframework.context.MessageSourceAwaresetMessageSource 
    • getAllowSessionCreation

      protected boolean getAllowSessionCreation()
    • setAllowSessionCreation

      public void setAllowSessionCreation(boolean allowSessionCreation)
    • setSessionAuthenticationStrategy

      public void setSessionAuthenticationStrategy(SessionAuthenticationStrategy sessionStrategy)
      認証リクエストが AuthenticationManager によって正常に処理された直後に呼び出されるセッション処理戦略。たとえば、セッション固定攻撃を防ぐためにセッション識別子の変更を処理するために使用されます。
      パラメーター:
      sessionStrategy - 使用する実装。設定しない場合、null 実装が使用されます。
    • setAuthenticationSuccessHandler

      public void setAuthenticationSuccessHandler(AuthenticationSuccessHandler successHandler)
      成功した認証を処理するために使用される戦略を設定します。デフォルトでは、SavedRequestAwareAuthenticationSuccessHandler が使用されます。
    • setAuthenticationFailureHandler

      public void setAuthenticationFailureHandler(AuthenticationFailureHandler failureHandler)
    • setSecurityContextRepository

      public void setSecurityContextRepository(SecurityContextRepository securityContextRepository)
      認証が成功したときに SecurityContext を保存するように SecurityContextRepository を設定します。デフォルトのアクションは、SecurityContext を保存しないことです。
      パラメーター:
      securityContextRepository - 使用する SecurityContextRepository。null にはできません。
    • setSecurityContextHolderStrategy

      public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy)
      使用する SecurityContextHolderStrategy を設定します。デフォルトのアクションは、SecurityContextHolder に格納されている SecurityContextHolderStrategy を使用することです。
      導入:
      5.8
    • getSuccessHandler

      protected AuthenticationSuccessHandler getSuccessHandler()
    • getFailureHandler

      protected AuthenticationFailureHandler getFailureHandler()