クラス BasicAuthenticationFilter
- 実装されているすべてのインターフェース:
jakarta.servlet.Filter、org.springframework.beans.factory.Aware、org.springframework.beans.factory.BeanNameAware、org.springframework.beans.factory.DisposableBean、org.springframework.beans.factory.InitializingBean、org.springframework.context.EnvironmentAware、org.springframework.core.env.EnvironmentCapable、org.springframework.web.context.ServletContextAware
SecurityContextHolder に入れます。このフィルターが処理するように設計されているものの詳細な背景については、RFC 1945、セクション 11.1 を参照してください。HTTP リクエストで提示されたレルム名は無視されます。
要約すると、このフィルターは、Basic の認証スキームと Base64 エンコードされた username:password トークンを使用して、Authorization の HTTP リクエストヘッダーを持つすべてのリクエストを処理します。例: ユーザー "Aladdin" をパスワード「open ゴマ」で認証するには、次のヘッダーが表示されます。
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
このフィルターを使用して、リモート認証プロトコルクライアント(Hessian や SOAP など)と標準ユーザーエージェント(Internet Explorer や Netscape など)の両方に BASIC 認証サービスを提供できます。
認証が成功すると、結果の Authentication オブジェクトが SecurityContextHolder に配置されます。
認証に失敗し、ignoreFailure が false (デフォルト) の場合、AuthenticationEntryPoint 実装が呼び出されます (ignoreFailure プロパティが true に設定されていない限り)。通常、これは BasicAuthenticationEntryPoint である必要があり、ユーザーは BASIC 認証を介して再度認証するように求められます。
基本認証は、シンプルで広く導入されているため、魅力的なプロトコルです。ただし、パスワードは平文で送信されるため、多くの状況では望ましくありません。
RememberMeServices が設定されている場合、このフィルターは、remember-me の詳細をクライアントに自動的に送信します。後続のリクエストは、remember-me メカニズムを使用して認証されるため、BASIC 認証ヘッダーを提示する必要はありません。
フィールドのサマリー
クラス org.springframework.web.filter.OncePerRequestFilter から継承されたフィールド
ALREADY_FILTERED_SUFFIXクラス org.springframework.web.filter.GenericFilterBean から継承されたフィールド
loggerコンストラクターの概要
コンストラクターコンストラクター説明BasicAuthenticationFilter(AuthenticationManager authenticationManager) 提供されたAuthenticationManagerに対して認証し、失敗した認証試行を無視するインスタンスを作成し、リクエストがフィルターチェーンを下って進むことを許可します。BasicAuthenticationFilter(AuthenticationManager authenticationManager, AuthenticationEntryPoint authenticationEntryPoint) 提供されたAuthenticationManagerに対して認証し、提供されたAuthenticationEntryPointを使用して認証の失敗を処理するインスタンスを作成します。方法の概要
修飾子と型メソッド説明voidprotected booleanauthenticationIsRequired(StringSE username) protected voiddoFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain) protected @Nullable AuthenticationEntryPointprotected AuthenticationManagerprotected StringSEgetCredentialsCharset(jakarta.servlet.http.HttpServletRequest httpRequest) protected booleanprotected voidonSuccessfulAuthentication(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Authentication authResult) protected voidonUnsuccessfulAuthentication(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, AuthenticationException failed) voidsetAuthenticationConverter(AuthenticationConverter authenticationConverter) 使用するAuthenticationConverterを設定します。voidsetAuthenticationDetailsSource(AuthenticationDetailsSource<jakarta.servlet.http.HttpServletRequest, ?> authenticationDetailsSource) 使用するAuthenticationDetailsSourceを設定します。voidsetCredentialsCharset(StringSE credentialsCharset) 資格情報をStringSE にデコードするときに使用する文字セットを設定します。voidsetMfaEnabled(boolean mfaEnabled) 多要素認証 (MFA) サポートを有効にします。voidsetRememberMeServices(RememberMeServices rememberMeServices) voidsetSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) 使用するSecurityContextHolderStrategyを設定します。voidsetSecurityContextRepository(SecurityContextRepository securityContextRepository) 認証が成功したときにSecurityContextを保存するようにSecurityContextRepositoryを設定します。クラス org.springframework.web.filter.OncePerRequestFilter から継承されたメソッド
doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterAsyncDispatch, shouldNotFilterErrorDispatchクラス org.springframework.web.filter.GenericFilterBean から継承されたメソッド
addRequiredProperty, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
コンストラクターの詳細
BasicAuthenticationFilter
提供されたAuthenticationManagerに対して認証し、失敗した認証試行を無視するインスタンスを作成し、リクエストがフィルターチェーンを下って進むことを許可します。- パラメーター:
authenticationManager- 認証リクエストを送信する Bean
BasicAuthenticationFilter
public BasicAuthenticationFilter(AuthenticationManager authenticationManager, AuthenticationEntryPoint authenticationEntryPoint) 提供されたAuthenticationManagerに対して認証し、提供されたAuthenticationEntryPointを使用して認証の失敗を処理するインスタンスを作成します。- パラメーター:
authenticationManager- 認証リクエストを送信する BeanauthenticationEntryPoint- 認証が失敗したときに呼び出されます。通常はBasicAuthenticationEntryPointのインスタンスです。
メソッドの詳細
setSecurityContextRepository
認証が成功したときにSecurityContextを保存するようにSecurityContextRepositoryを設定します。デフォルトのアクションは、SecurityContextを保存しないことです。- パラメーター:
securityContextRepository- 使用するSecurityContextRepository。null にはできません。
setMfaEnabled
public void setMfaEnabled(boolean mfaEnabled) 多要素認証 (MFA) サポートを有効にします。- パラメーター:
mfaEnabled- MFA サポートを有効にする場合は true、無効にする場合は false です。デフォルトは false です。
setAuthenticationConverter
使用するAuthenticationConverterを設定します。デフォルトはBasicAuthenticationConverter- パラメーター:
authenticationConverter- 使用するコンバーター- 導入:
- 6.2
afterPropertiesSet
public void afterPropertiesSet()- 次で指定:
- インターフェース
org.springframework.beans.factory.InitializingBeanのafterPropertiesSet - オーバーライド:
- クラス
org.springframework.web.filter.GenericFilterBeanのafterPropertiesSet
doFilterInternal
protected void doFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain) throws IOExceptionSE, jakarta.servlet.ServletException - 次で指定:
- クラス
org.springframework.web.filter.OncePerRequestFilterのdoFilterInternal - 例外:
IOExceptionSEjakarta.servlet.ServletException
authenticationIsRequired
onSuccessfulAuthentication
protected void onSuccessfulAuthentication(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Authentication authResult) throws IOExceptionSE onUnsuccessfulAuthentication
protected void onUnsuccessfulAuthentication(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, AuthenticationException failed) throws IOExceptionSE getAuthenticationEntryPoint
getAuthenticationManager
isIgnoreFailure
protected boolean isIgnoreFailure()setSecurityContextHolderStrategy
public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) 使用するSecurityContextHolderStrategyを設定します。デフォルトのアクションは、SecurityContextHolderに格納されているSecurityContextHolderStrategyを使用することです。- 導入:
- 5.8
setAuthenticationDetailsSource
public void setAuthenticationDetailsSource(AuthenticationDetailsSource<jakarta.servlet.http.HttpServletRequest, ?> authenticationDetailsSource) 使用するAuthenticationDetailsSourceを設定します。デフォルトでは、WebAuthenticationDetailsSourceを使用するように設定されています。この構成は、authenticationConverterがBasicAuthenticationConverterに設定されている場合にのみ適用されることに注意してください。別の実装を利用している場合は、その実装で認証の詳細を手動で指定する必要があります。- パラメーター:
authenticationDetailsSource- 使用するAuthenticationDetailsSource。
setRememberMeServices
setCredentialsCharset
資格情報をStringSE にデコードするときに使用する文字セットを設定します。デフォルトでは、UTF-8に設定されています。この構成は、authenticationConverterがBasicAuthenticationConverterに設定されている場合にのみ適用されることに注意してください。別の実装を利用している場合は、その実装の文字セットを手動で指定する必要があります。- パラメーター:
credentialsCharset- 使用する文字セット。
getCredentialsCharset