Spring Boot統合Spring Security略記-匿名認証(六)

2423 ワード

newの無語転載はオリジナルの出所を明記してください.ありがとうございます.
Spring Security学習カタログ
匿名アクセスのユーザに対して、Spring Securityは、その匿名のAnonymousAuthentication TokenをSecurityContact Holderに保存することをサポートしています.これはいわゆる匿名認証です.
Spring Security 3.0以降は、匿名のサポートが自動的に提供されますが、基本的な認識のためにここに記録されています.
匿名認証に関するクラスは三つあります.
  • AnonymousAuthenticationToken
  • AnonymousAuthenticationProvider
  • AnonymousAuthenticationFilter
  • 設定
        public AnonymousAuthenticationFilter anonymousAuthenticationFilter(){
            AnonymousAuthenticationFilter anonymousAuthenticationFilter = new AnonymousAuthenticationFilter("foobar");
            return anonymousAuthenticationFilter;
        }
    
        @Bean
        public AnonymousAuthenticationProvider anonymousAuthenticationProvider(){
            return new AnonymousAuthenticationProvider("foobar");
        }
    
    keyは"foobar"に設定され、keyはAuthentication FilterとAuthentication Providerの間で共有される値を指定する.匿名のユーザ名と権限は、デフォルト値anonymousUserを使用してROLE_ANONYMOUS.匿名アクセスをテストするための権限パスを追加します.
      .antMatchers("/anonymous/**").hasRole("ANONYMOUS")
    
    その後、プロジェクトを起動し、http://localhost:8080/anonymous/123にアクセスし、404に戻ると構成が成功します.
    Authentication Trust Resolaver
    匿名認証検証を完了したのはAuthenticationTrustResolverインターフェースと対応するAuthenticationTrustResolverImplである.インターフェースは、isAnonymous(Authentication)が匿名認証ユーザ本体であるかどうかを確認するAuthentication方法を提供する.
       /**
         * Indicates whether the passed Authentication token represents an
         * anonymous user. Typically the framework will call this method if it is trying to
         * decide whether an AccessDeniedException should result in a final
         * rejection (i.e. as would be the case if the principal was non-anonymous/fully
         * authenticated) or direct the principal to attempt actual authentication (i.e. as
         * would be the case if the Authentication was merely anonymous).
         *
         * @param authentication to test (may be null in which case the method
         * will always return false)
         *
         * @return true the passed authentication token represented an anonymous
         * principal, false otherwise
         */
        boolean isAnonymous(Authentication authentication);