话说Spring Security权限管理(源码详解)

时间:2021-12-12 11:22:00

最近项目需要用到spring security的权限控制,故花了点时间简单的去看了一下其权限控制相关的源码(版本为4.2)。

accessdecisionmanager

spring security是通过accessdecisionmanager进行授权管理的,先来张官方图镇楼。

话说Spring Security权限管理(源码详解)

accessdecisionmanager

accessdecisionmanager 接口定义了如下方法:

?
1
2
3
4
5
6
7
//调用accessdecisionvoter进行投票(关键方法)
void decide(authentication authentication, object object,
    collection<configattribute> configattributes) throws accessdeniedexception,
    insufficientauthenticationexception;
 
boolean supports(configattribute attribute);
boolean supports(class clazz);

接下来看看它的实现类的具体实现:

affirmativebased

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public void decide(authentication authentication, object object,
    collection<configattribute> configattributes) throws accessdeniedexception {
  int deny = 0;
 
  for (accessdecisionvoter voter : getdecisionvoters()) {
    //调用accessdecisionvoter进行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) {
    //如果有两个及以上accessdecisionvoter(姑且称之为投票者吧)都投access_denied,则直接就不通过了
    throw new accessdeniedexception(messages.getmessage(
        "abstractaccessdecisionmanager.accessdenied", "access is denied"));
  }
 
  // to get this far, every accessdecisionvoter abstained
  checkallowifallabstaindecisions();
}

通过以上代码可直接看到affirmativebased的策略:

  • 只要有投通过(access_granted)票,则直接判为通过。
  • 如果没有投通过票且反对(access_denied)票在两个及其以上的,则直接判为不通过。

unanimousbased

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()) {
      //配置的投票者进行投票
      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:
        //只要有投票者投反对票就立马判为无权访问
        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) {
    //如果没反对票且有通过票,那么就判为通过
    return;
  }
 
  // to get this far, every accessdecisionvoter abstained
  checkallowifallabstaindecisions();
}

由此可见unanimousbased的策略:

  • 无论多少投票者投了多少通过(access_granted)票,只要有反对票(access_denied),那都判为不通过。
  • 如果没有反对票且有投票者投了通过票,那么就判为通过。

consensusbased

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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()) {
    //配置的投票者进行投票
    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) {
    //通过的票数大于反对的票数则判为通过
    return;
  }
 
  if (deny > grant) {
    //通过的票数小于反对的票数则判为不通过
    throw new accessdeniedexception(messages.getmessage(
        "abstractaccessdecisionmanager.accessdenied", "access is denied"));
  }
 
  if ((grant == deny) && (grant != 0)) {
    //this.allowifequalgranteddenieddecisions默认为true
    //通过的票数和反对的票数相等,则可根据配置allowifequalgranteddenieddecisions进行判断是否通过
    if (this.allowifequalgranteddenieddecisions) {
      return;
    }
    else {
      throw new accessdeniedexception(messages.getmessage(
          "abstractaccessdecisionmanager.accessdenied", "access is denied"));
    }
  }
 
  // to get this far, every accessdecisionvoter abstained
  checkallowifallabstaindecisions();
}

由此可见,consensusbased的策略:

  • 通过的票数大于反对的票数则判为通过。
  • 通过的票数小于反对的票数则判为不通过。
  • 通过的票数和反对的票数相等,则可根据配置allowifequalgranteddenieddecisions(默认为true)进行判断是否通过。

到此,应该明白affirmativebased、unanimousbased、consensusbased三者的区别了吧,spring security默认使用的是affirmativebased, 如果有需要,可配置为其它两个,也可自己去实现。

投票者

以上accessdecisionmanager的实现类都只是对权限(投票)进行管理(策略的实现),具体投票(vote)的逻辑是通过调用accessdecisionvoter的子类(投票者)的vote方法实现的。spring security默认注册了rolevoter和authenticatedvoter两个投票者。下面来看看其源码。

accessdecisionmanager

?
1
2
3
4
5
boolean supports(configattribute attribute);
boolean supports(class<?> clazz);
//核心方法,此方法由上面介绍的的accessdecisionmanager调用,子类实现此方法进行投票。
int vote(authentication authentication, s object,
    collection<configattribute> attributes);

rolevoter

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
private string roleprefix = "role_";
 
//只处理role_开头的(可通过配置roleprefix的值进行改变)
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) {
    //用户没通过认证,则投反对票
    return access_denied;
  }
  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;
        }
      }
    }
  }
  //如果处理过,但没投通过票,则为反对票,如果没处理过,那么视为弃权(access_abstain)。
  return result;

很简单吧,同时,我们还可以通过实现accessdecisionmanager来扩展自己的voter。但是,要实现这个,我们还必须得弄清楚attributes这个参数是从哪儿来的,这个是个很关键的参数啊。通过一张官方图能很清晰的看出这个问题来:

话说Spring Security权限管理(源码详解)

接下来,就看看accessdecisionmanager的调用者abstractsecurityinterceptor。

abstractsecurityinterceptor

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
...
//上面说过默认是affirmativebased,可配置
private accessdecisionmanager accessdecisionmanager;
...
protected interceptorstatustoken beforeinvocation(object object) {
  ...
  //抽象方法,子类实现,但由此也可看出configattribute是由securitymetadatasource(实际上,默认是defaultfilterinvocationsecuritymetadatasource)获取。
  collection<configattribute> attributes = this.obtainsecuritymetadatasource()
      .getattributes(object);
  ...
  //获取当前认证过的用户信息
  authentication authenticated = authenticateifrequired();
 
  try {
    //调用accessdecisionmanager
    this.accessdecisionmanager.decide(authenticated, object, attributes);
  }
  catch (accessdeniedexception accessdeniedexception) {
    publishevent(new authorizationfailureevent(object, attributes, authenticated,
        accessdeniedexception));
 
    throw accessdeniedexception;
  }
  ...  
}
 
public abstract securitymetadatasource obtainsecuritymetadatasource();

以上方法都是由abstractsecurityinterceptor的子类(默认是filtersecurityinterceptor)调用,那就再看看吧:

filtersecurityinterceptor

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
...
//securitymetadatasource的实现类,由此可见,可通过外部配置。这也说明我们可以通过自定义securitymetadatasource的实现类来扩展出自己实际需要的configattribute
private filterinvocationsecuritymetadatasource securitymetadatasource;
...
//入口
public void dofilter(servletrequest request, servletresponse response,
    filterchain chain) throws ioexception, servletexception {
  filterinvocation fi = new filterinvocation(request, response, chain);
  //关键方法
  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);
    }
    //在这儿调用了父类(abstractsecurityinterceptor)的方法, 也就调用了accessdecisionmanager
    interceptorstatustoken token = super.beforeinvocation(fi);
 
    try {
      fi.getchain().dofilter(fi.getrequest(), fi.getresponse());
    }
    finally {
      super.finallyinvocation(token);
    }
    //完了再执行(父类的方法),一前一后,aop无处不在啊
    super.afterinvocation(token, null);
  }
}

好啦,到此应该对于spring security的权限管理比较清楚了。看完这个,不知你是否能扩展出一套适合自己需求的权限需求来呢,如果还不太清楚,那也没关系,下篇就实战一下,根据它来开发一套自己的权限体系。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/dongying/p/6106855.html