溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

話說Spring Security權(quán)限管理(源碼詳解)

發(fā)布時(shí)間:2020-10-08 00:42:49 來源:腳本之家 閱讀:311 作者:南軻夢(mèng) 欄目:編程語言

最近項(xiàng)目需要用到Spring Security的權(quán)限控制,故花了點(diǎn)時(shí)間簡(jiǎn)單的去看了一下其權(quán)限控制相關(guān)的源碼(版本為4.2)。

AccessDecisionManager

spring security是通過AccessDecisionManager進(jìn)行授權(quán)管理的,先來張官方圖鎮(zhèn)樓。

話說Spring Security權(quán)限管理(源碼詳解)

AccessDecisionManager

AccessDecisionManager 接口定義了如下方法:

//調(diào)用AccessDecisionVoter進(jìn)行投票(關(guān)鍵方法)
void decide(Authentication authentication, Object object,
    Collection<ConfigAttribute> configAttributes) throws AccessDeniedException,
    InsufficientAuthenticationException;

boolean supports(ConfigAttribute attribute);
boolean supports(Class clazz);

接下來看看它的實(shí)現(xiàn)類的具體實(shí)現(xiàn):

AffirmativeBased

public void decide(Authentication authentication, Object object,
    Collection<ConfigAttribute> configAttributes) throws AccessDeniedException {
  int deny = 0;

  for (AccessDecisionVoter voter : getDecisionVoters()) {
    //調(diào)用AccessDecisionVoter進(jìn)行vote(我們姑且稱之為投票吧),后面再看vote的源碼。
    int result = voter.vote(authentication, object, configAttributes);

    if (logger.isDebugEnabled()) {
      logger.debug("Voter: " + voter + ", returned: " + result);
    }
    
    switch (result) {
    case AccessDecisionVoter.ACCESS_GRANTED://值為1
      //只要有voter投票為ACCESS_GRANTED,則通過
      return;

    case AccessDecisionVoter.ACCESS_DENIED://值為-1
      deny++;

      break;

    default:
      break;
    }
  }

  if (deny > 0) {
    //如果有兩個(gè)及以上AccessDecisionVoter(姑且稱之為投票者吧)都投ACCESS_DENIED,則直接就不通過了
    throw new AccessDeniedException(messages.getMessage(
        "AbstractAccessDecisionManager.accessDenied", "Access is denied"));
  }

  // To get this far, every AccessDecisionVoter abstained
  checkAllowIfAllAbstainDecisions();
}

通過以上代碼可直接看到AffirmativeBased的策略:

  • 只要有投通過(ACCESS_GRANTED)票,則直接判為通過。
  • 如果沒有投通過票且反對(duì)(ACCESS_DENIED)票在兩個(gè)及其以上的,則直接判為不通過。

UnanimousBased

public void decide(Authentication authentication, Object object,
    Collection<ConfigAttribute> attributes) throws AccessDeniedException {

  int grant = 0;
  int abstain = 0;

  List<ConfigAttribute> singleAttributeList = new ArrayList<ConfigAttribute>(1);
  singleAttributeList.add(null);

  for (ConfigAttribute attribute : attributes) {
    singleAttributeList.set(0, attribute);

    for (AccessDecisionVoter voter : getDecisionVoters()) {
      //配置的投票者進(jìn)行投票
      int result = voter.vote(authentication, object, singleAttributeList);

      if (logger.isDebugEnabled()) {
        logger.debug("Voter: " + voter + ", returned: " + result);
      }

      switch (result) {
      case AccessDecisionVoter.ACCESS_GRANTED:
        grant++;

        break;

      case AccessDecisionVoter.ACCESS_DENIED:
        //只要有投票者投反對(duì)票就立馬判為無權(quán)訪問
        throw new AccessDeniedException(messages.getMessage(
            "AbstractAccessDecisionManager.accessDenied",
            "Access is denied"));

      default:
        abstain++;

        break;
      }
    }
  }

  // To get this far, there were no deny votes
  if (grant > 0) {
    //如果沒反對(duì)票且有通過票,那么就判為通過
    return;
  }

  // To get this far, every AccessDecisionVoter abstained
  checkAllowIfAllAbstainDecisions();
}

由此可見UnanimousBased的策略:

  • 無論多少投票者投了多少通過(ACCESS_GRANTED)票,只要有反對(duì)票(ACCESS_DENIED),那都判為不通過。
  • 如果沒有反對(duì)票且有投票者投了通過票,那么就判為通過。

ConsensusBased

public void decide(Authentication authentication, Object object,
    Collection<ConfigAttribute> configAttributes) throws AccessDeniedException {
  int grant = 0;
  int deny = 0;
  int abstain = 0;

  for (AccessDecisionVoter voter : getDecisionVoters()) {
    //配置的投票者進(jìn)行投票
    int result = voter.vote(authentication, object, configAttributes);

    if (logger.isDebugEnabled()) {
      logger.debug("Voter: " + voter + ", returned: " + result);
    }

    switch (result) {
    case AccessDecisionVoter.ACCESS_GRANTED:
      grant++;

      break;

    case AccessDecisionVoter.ACCESS_DENIED:
      deny++;

      break;

    default:
      abstain++;

      break;
    }
  }

  if (grant > deny) {
    //通過的票數(shù)大于反對(duì)的票數(shù)則判為通過
    return;
  }

  if (deny > grant) {
    //通過的票數(shù)小于反對(duì)的票數(shù)則判為不通過
    throw new AccessDeniedException(messages.getMessage(
        "AbstractAccessDecisionManager.accessDenied", "Access is denied"));
  }

  if ((grant == deny) && (grant != 0)) {
    //this.allowIfEqualGrantedDeniedDecisions默認(rèn)為true
    //通過的票數(shù)和反對(duì)的票數(shù)相等,則可根據(jù)配置allowIfEqualGrantedDeniedDecisions進(jìn)行判斷是否通過
    if (this.allowIfEqualGrantedDeniedDecisions) {
      return;
    }
    else {
      throw new AccessDeniedException(messages.getMessage(
          "AbstractAccessDecisionManager.accessDenied", "Access is denied"));
    }
  }

  // To get this far, every AccessDecisionVoter abstained
  checkAllowIfAllAbstainDecisions();
}

由此可見,ConsensusBased的策略:

  • 通過的票數(shù)大于反對(duì)的票數(shù)則判為通過。
  • 通過的票數(shù)小于反對(duì)的票數(shù)則判為不通過。
  • 通過的票數(shù)和反對(duì)的票數(shù)相等,則可根據(jù)配置allowIfEqualGrantedDeniedDecisions(默認(rèn)為true)進(jìn)行判斷是否通過。

到此,應(yīng)該明白AffirmativeBased、UnanimousBased、ConsensusBased三者的區(qū)別了吧,spring security默認(rèn)使用的是AffirmativeBased, 如果有需要,可配置為其它兩個(gè),也可自己去實(shí)現(xiàn)。

投票者

以上AccessDecisionManager的實(shí)現(xiàn)類都只是對(duì)權(quán)限(投票)進(jìn)行管理(策略的實(shí)現(xiàn)),具體投票(vote)的邏輯是通過調(diào)用AccessDecisionVoter的子類(投票者)的vote方法實(shí)現(xiàn)的。spring security默認(rèn)注冊(cè)了RoleVoter和AuthenticatedVoter兩個(gè)投票者。下面來看看其源碼。

AccessDecisionManager

boolean supports(ConfigAttribute attribute);
boolean supports(Class<?> clazz);
//核心方法,此方法由上面介紹的的AccessDecisionManager調(diào)用,子類實(shí)現(xiàn)此方法進(jìn)行投票。
int vote(Authentication authentication, S object,
    Collection<ConfigAttribute> attributes);

RoleVoter

private String rolePrefix = "ROLE_";

//只處理ROLE_開頭的(可通過配置rolePrefix的值進(jìn)行改變)
public boolean supports(ConfigAttribute attribute) {
  if ((attribute.getAttribute() != null)
      && attribute.getAttribute().startsWith(getRolePrefix())) {
    return true;
  }
  else {
    return false;
  }
}

public int vote(Authentication authentication, Object object,
    Collection<ConfigAttribute> attributes) {
    
  if(authentication == null) {
    //用戶沒通過認(rèn)證,則投反對(duì)票
    return ACCESS_DENIED;
  }
  int result = ACCESS_ABSTAIN;
  //獲取用戶實(shí)際的權(quán)限
  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())) {
          //權(quán)限匹配則投通過票
          return ACCESS_GRANTED;
        }
      }
    }
  }
  //如果處理過,但沒投通過票,則為反對(duì)票,如果沒處理過,那么視為棄權(quán)(ACCESS_ABSTAIN)。
  return result;
}  

很簡(jiǎn)單吧,同時(shí),我們還可以通過實(shí)現(xiàn)AccessDecisionManager來擴(kuò)展自己的voter。但是,要實(shí)現(xiàn)這個(gè),我們還必須得弄清楚attributes這個(gè)參數(shù)是從哪兒來的,這個(gè)是個(gè)很關(guān)鍵的參數(shù)啊。通過一張官方圖能很清晰的看出這個(gè)問題來:

話說Spring Security權(quán)限管理(源碼詳解)

接下來,就看看AccessDecisionManager的調(diào)用者AbstractSecurityInterceptor。

AbstractSecurityInterceptor

...
//上面說過默認(rèn)是AffirmativeBased,可配置
private AccessDecisionManager accessDecisionManager;
...
protected InterceptorStatusToken beforeInvocation(Object object) {
  ...
  //抽象方法,子類實(shí)現(xiàn),但由此也可看出ConfigAttribute是由SecurityMetadataSource(實(shí)際上,默認(rèn)是DefaultFilterInvocationSecurityMetadataSource)獲取。
  Collection<ConfigAttribute> attributes = this.obtainSecurityMetadataSource()
      .getAttributes(object);
  ...
  //獲取當(dāng)前認(rèn)證過的用戶信息
  Authentication authenticated = authenticateIfRequired();

  try {
    //調(diào)用AccessDecisionManager
    this.accessDecisionManager.decide(authenticated, object, attributes);
  }
  catch (AccessDeniedException accessDeniedException) {
    publishEvent(new AuthorizationFailureEvent(object, attributes, authenticated,
        accessDeniedException));

    throw accessDeniedException;
  }
  ...   
}

public abstract SecurityMetadataSource obtainSecurityMetadataSource();

以上方法都是由AbstractSecurityInterceptor的子類(默認(rèn)是FilterSecurityInterceptor)調(diào)用,那就再看看吧:

FilterSecurityInterceptor

...
//SecurityMetadataSource的實(shí)現(xiàn)類,由此可見,可通過外部配置。這也說明我們可以通過自定義SecurityMetadataSource的實(shí)現(xiàn)類來擴(kuò)展出自己實(shí)際需要的ConfigAttribute
private FilterInvocationSecurityMetadataSource securityMetadataSource; 
...
//入口
public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
  FilterInvocation fi = new FilterInvocation(request, response, chain);
  //關(guān)鍵方法
  invoke(fi);
}

public void invoke(FilterInvocation fi) throws IOException, ServletException {
  if ((fi.getRequest() != null)
      && (fi.getRequest().getAttribute(FILTER_APPLIED) != null)
      && observeOncePerRequest) {
    // filter already applied to this request and user wants us to observe
    // once-per-request handling, so don't re-do security checking
    fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
  }
  else {
    // first time this request being called, so perform security checking
    if (fi.getRequest() != null) {
      fi.getRequest().setAttribute(FILTER_APPLIED, Boolean.TRUE);
    }
    //在這兒調(diào)用了父類(AbstractSecurityInterceptor)的方法, 也就調(diào)用了accessDecisionManager
    InterceptorStatusToken token = super.beforeInvocation(fi);

    try {
      fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
    }
    finally {
      super.finallyInvocation(token);
    }
    //完了再執(zhí)行(父類的方法),一前一后,AOP無處不在啊
    super.afterInvocation(token, null);
  }
}

好啦,到此應(yīng)該對(duì)于Spring Security的權(quán)限管理比較清楚了??赐赀@個(gè),不知你是否能擴(kuò)展出一套適合自己需求的權(quán)限需求來呢,如果還不太清楚,那也沒關(guān)系,下篇就實(shí)戰(zhàn)一下,根據(jù)它來開發(fā)一套自己的權(quán)限體系。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI