spring securityカスタム意思決定マネージャ


まずSpringの意思決定マネージャを紹介します。そのインターフェースはAccess DecisionManagerで、抽象的なタイプはAbstractAccess DecisionManagerです。私たちがカスタマイズしたいのなら、直接にインターフェースを実現するよりも抽象的なクラスを引き継ぐのが一般的です。
Springには投票器の概念が導入されています。アクセス権限の有無は最終的に投票器によって決定されます。最も一般的な投票機はRoleVoterで、RoleVoterに権限のプレフィックスが定義されています。まずSpringはRoleVoterにどのように承認されているかを見てください。

public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) { 
  int result = ACCESS_ABSTAIN; 
  Collection<? extends GrantedAuthority> authorities = extractAuthorities(authentication); 
  for (ConfigAttribute attribute : attributes) { 
    if (this.supports(attribute)) { 
      result = ACCESS_DENIED; 
      // Attempt to find a matching granted authority 
      for (GrantedAuthority authority : authorities) { 
        if (attribute.getAttribute().equals(authority.getAuthority())) { 
          return ACCESS_GRANTED; 
        } 
      } 
    } 
  } 
  return result; 
} 
Collection<? extends GrantedAuthority> extractAuthorities(Authentication authentication) { 
  return authentication.getAuthorities(); 
} 
Authenticationではユーザおよびユーザ権限情報であり、atributesはリソースにアクセスするために必要な権限であり、その後、ユーザがリソースにアクセスするために必要な権限があるかどうかを循環的に判断し、ある場合はACCESS_に戻る。GRANTEDは、通俗的には権限があります。
Springは3つの政策決定マネージャを提供しています。この3つのマネージャはどのように仕事をしていますか?Spring Securityのソースコードを確認してください。
Affirmative eBased 一票が通ると、一つの投票機が通ると訪問が許可されます。
コンセスベース 半分以上の投票器が通過してこそ、リソースへのアクセスが許可されます。
ユニコムBased すべての投票機が通過してからアクセスが許可されます。
以下に簡単なカスタム意思決定マネージャを実装します。この決定マネージャは投票機を使用していません。

public class DefaultAccessDecisionManager extends AbstractAccessDecisionManager { 
  public void decide( Authentication authentication, Object object,  
      Collection<ConfigAttribute> configAttributes)  
    throws AccessDeniedException, InsufficientAuthenticationException{ 
    SysUser user = (SysUser)authentication.getPrincipal(); 
    logger.info("        "+user.getUsername()); 
    //                   
    if( configAttributes == null ) { 
      return ; 
    } 
    Iterator<ConfigAttribute> ite = configAttributes.iterator(); 
    //  configAttributes              
    while( ite.hasNext()){ 
      ConfigAttribute ca = ite.next(); 
      String needRole = ((SecurityConfig)ca).getAttribute(); 
      //ga           。 needRole                。 
      for( GrantedAuthority ga: authentication.getAuthorities()){ 
        if(needRole.trim().equals(ga.getAuthority().trim())){ 
          return; 
        } 
      } 
    } 
    throw new AccessDeniedException(""); 
  } 
} 
 decideこの方法には戻り値がありません。ライセンスがないときにAccess DenieExceptionを投げ出す必要があります。
あるリソースにアクセスするには2つ以上の権限が必要な場合は、Access DecisionVoterをカスタマイズすることで実現します。これも簡単です。ここでは説明しなくてもいいです。このような表式をページで使うにはWebExpressionVoterを注入する必要があります。
SprigSecurityでユーザー定義のパーミッションプレフィックス
権限のプレフィックスはデフォルトはROLE_で、ネット上の多くの例は、直接配置ファイルに下の配置を加えればいいということです。

<bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter"> 
  <property name="rolePrefix" value="AUTH_"></property> 
</bean> 
測定がうまくいかないのは、私たちが配置した問題ではなく、http aut-config=「true」Springを配置したからです。すでにAccess DecisionManagerを初期化しました。配置が前になってもだめです。この初期化はSpring自身が完成したので、あなたが配置したroleVoterをAccess Desiongerに注入していません。手差しでAccess DecisionManagerを注入しましょう。
httpではaccess-decision-manager-ref属性があります。Access DecisionManagerに手動で注入できます。以下は詳細な配置です。

<sec:http auto-config="true" access-decision-manager-ref="accessDecisionManager"> 
  <sec:access-denied-handler ref="accessDeniedHandler"/> 
  <sec:session-management invalid-session-url="/login.jsp" /> 
  <sec:intercept-url pattern="/app.jsp" access="AUTH_GG_FBGBGG"/> 
  <sec:intercept-url pattern="/**" access="IS_AUTHENTICATED_FULLY" /> 
  <sec:form-login login-page="/login.jsp" authentication-failure-url="/login.jsp" 
    default-target-url="/index.jsp"/> 
</sec:http> 
<bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased"> 
  <constructor-arg name="decisionVoters"> 
    <list> 
      <ref bean="roleVoter"/> 
      <ref bean="authenticatedVoter"/> 
    </list> 
  </constructor-arg> 
  <property name="messageSource" ref="messageSource"></property> 
</bean> 
<bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter"> 
  <property name="rolePrefix" value=""></property> 
</bean> 
<bean id="authenticatedVoter" class="org.springframework.security.access.vote.AuthenticatedVoter" /> 
ここではカスタムのAccess DecisionManagerを使いません。直接SpringのAffirmative Basedを使います。Spring自身が提供しているこれらの政策決定マネージャはもう強大ですから。
構成は簡単で、権限のプレフィックスを変更するには、roleVoterのrolePrefixを変更するだけでいいです。プレフィックスなしで「」にします。
authenticated VoterはIS_をサポートするためです。AUTHENTICATEDという認証は、authenticated Voterが提供する3つの認証です。
IS_AUTHENTICATED_ANONYMOUSLYは匿名のユーザーを許可します。
IS_AUTHENTICATED_FULLYはユーザー登録を許可します。
IS_AUTHENTICATED_REMEMBEREDはログインユーザとremember Meユーザのログインを許可します。
締め括りをつける
以上は小编が皆さんに绍介したspring securityカスタム方策决定管理器です。皆さんに何かお聞きしたいことがあれば、メッセージをください。小编はすぐにご返事します。ここでも私たちのサイトを応援してくれてありがとうございます。