前言
Spring Security里有一个重点功能是权限认证,权限认证用于判断当前认证主体是否有访问当前资源的权限。
核心接口是AccessDecisionManager,核心方法是
void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes)
throws AccessDeniedException, InsufficientAuthenticationException;
下面是他的类UML图,标准的spring bean的模式,一个接口,一个模板抽象类,一个实现方式。
代码
实际spring security提供了三种选举模式,
AffirmativeBased 只要有一个同意就通过,都不同意就抛异常,如果都弃权的话就看是否配置了默认通过。
整个方法里只有return,出错就抛异常,这个和之前的服务内部异常交互处理结果的处理方式相同。
不通过boolean来判断是否通过,不通过就抛异常。
public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes)
throws AccessDeniedException {
int deny = 0;
for (AccessDecisionVoter voter : getDecisionVoters()) {
int result = voter.vote(authentication, object, configAttributes);
switch (result) {
case AccessDecisionVoter.ACCESS_GRANTED:
return; // 一个同意就直接return了
case AccessDecisionVoter.ACCESS_DENIED:
deny++;
break;
default:
break;
}
}
if (deny > 0) {
throw new AccessDeniedException(
this.messages.getMessage("AbstractAccessDecisionManager.accessDenied", "Access is denied"));
}
// To get this far, every AccessDecisionVoter abstained
checkAllowIfAllAbstainDecisions();
}
其他两个,一个是全部同意才同意,一个是同意大于不同意就算同意,代码就不贴了。
其他
这里的manager——voter模式在security里有两个地方在用,一个是这里,另一个是provider认证的时候用。