Spring Security权限管理(源码) 杂谈

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

AccessDecisionManager

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

Spring Security权限管理(源码) 杂谈

AccessDecisionManager

AccessDecisionManager 接口定义了如下方法:

//调用AccessDecisionVoter进行投票(关键方法) void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException; boolean supports(ConfigAttribute attribute); boolean supports(Class clazz);

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

AffirmativeBased 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(); }

源码中,有个Collection configAttributes 参数,ConfigAttribute是什么? 这个其实是一个很灵活的东西,不同的情况代表不同的语义,比如在使用了角色控制的时候,传入的则可能是ROLE__XXX之类的,以便ROLE_VOTER使用。具体的后面在细说。

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

只要有投通过(ACCESS_GRANTED)票,则直接判为通过。

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

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()) { //配置的投票者进行投票 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 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)进行判断是否通过。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/7fed03a20d09c55f8ac396cb9a85d546.html