AccessDecisionVoter
AccessDecisionVoter主要的职责就是对它所对应的访问规则作出判断,当前的访问规则是否可以得到授权。

supports方法用于判断对于当前ConfigAttribute访问规则是否支持;如果支持的情况下,vote方法对其进行判断投票返回对应的授权结果。 最终的授权结果一共有三种,分别是同意、弃权和反对。当前一个访问可能存在多个规则的情况下,每一个AccessDecisionVoter投出自己的那一票,最终的投票结果是还是要看当前的投票规则,比如是超过1/3还是要过半数。而投票规则的判断则是被放置了在了AccessDecisionManager进行完成。
设计上的大原则:AccessDecisionVoter的实现是为了满足对应规则ConfigAttribute。大体上来说AccessDecisionVoter是与ConfigAttribute一一对应的。在客制化场景下被利用的SecurityConfig配置和他默认的两个AccessDecisionVoter:
- RoleVoter
- AuthenticatedVoter
用@Secured注解编写一个表达式:
@Secured("ROLE_USER")
@Secured("IS_AUTHENTICATED_ANONYMOUSLY")
AccessDecisionVoter和ConfigAttribute的关联关系是通过supports方法进行判断;
RoleVoter是Spring Security中默认基于角色规则核心组件。在UserDetailsService中创建用户都会需要设置对用用户的角色信息。在默认配置下用户的角色信息都是以"ROLE_"+角色名的形式存储的。 对应的在RoleVoter的supports方法中会对表达式是否以'ROLE_'开始作为对应启用规则的判断。如果规则表达式是以ROLE_开始的,RoleVoter则会去遍历对用Authentication是否存在对应的角色,如果存在则返回通过,如果不存在则返回拒绝。public class RoleVoter implements AccessDecisionVoter<Object> { private String rolePrefix = "ROLE_"; public String getRolePrefix() { return rolePrefix; } public void setRolePrefix(String rolePrefix) { this.rolePrefix = rolePrefix; } public boolean supports(ConfigAttribute attribute) { if ((attribute.getAttribute() != null) && attribute.getAttribute().startsWith(getRolePrefix())) { return true; } else { return false; } } }
@Secured("IS_AUTHENTICATED_FULLY")去限定用户是一个通过完全验证的用户,而不是通过RememberMe方式认证的用户。 在AuthenticatedVoter的supports方法中,便会判断当前的表达式是为他所支持的三种认证方法的访问控制:
IS_AUTHENTICATED_FULLY
IS_AUTHENTICATED_REMEMBERED
IS_AUTHENTICATED_ANONYMOUSLY
public class AuthenticatedVoter implements AccessDecisionVoter<Object> { // ~ Static fields/initializers // ===================================================================================== public static final String IS_AUTHENTICATED_FULLY = "IS_AUTHENTICATED_FULLY"; public static final String IS_AUTHENTICATED_REMEMBERED = "IS_AUTHENTICATED_REMEMBERED"; public static final String IS_AUTHENTICATED_ANONYMOUSLY = "IS_AUTHENTICATED_ANONYMOUSLY"; // ~ Instance fields // ================================================================================================ private AuthenticationTrustResolver authenticationTrustResolver = new AuthenticationTrustResolverImpl(); // ~ Methods // ======================================================================================================== private boolean isFullyAuthenticated(Authentication authentication) { return (!authenticationTrustResolver.isAnonymous(authentication) && !authenticationTrustResolver.isRememberMe(authentication)); } public boolean supports(ConfigAttribute attribute) { if ((attribute.getAttribute() != null) && (IS_AUTHENTICATED_FULLY.equals(attribute.getAttribute()) || IS_AUTHENTICATED_REMEMBERED.equals(attribute.getAttribute()) || IS_AUTHENTICATED_ANONYMOUSLY .equals(attribute.getAttribute()))) { return true; } else { return false; } } }
客制化实例:基于时间的AccessDecisionVoter
客制化一个基于时间的访问控制,在系统时间的分钟数是奇数的情况下才可以被访问,比如10点01分可以访问,但是10点02分则不可以被访问。
规则设计:拟定的规则名为"MINUTE_ODD",当方法级被注解了@Secured("MINUTE_ODD")情况下,表示当前方法只有在满足系统时间的分钟数为奇数下才可以被访问。
客制化MinuteBasedVoter
实现对应的suppors方法用于完成对拟定的规则的判断。当入参ConfigAttribute 的表达式属性与我们预设的"MINUTE_ODD"一致时,那么便返回true告知框架,MinuteBasedVoter需要对该规则进行vote的投票操作。
public class MinuteBasedVoter implements AccessDecisionVoter { public static final String IS_MINUTE_ODD= "MINUTE_ODD"; @Override public boolean supports(ConfigAttribute attribute) { if ((attribute.getAttribute() != null) && attribute.getAttribute().equals(IS_MINUTE_ODD)) { return true; } else { return false; } } @Override public boolean supports(Class clazz) { return true; } }
将vote的投票核心业务逻辑完成:当时间为奇数的时候则投赞同票,而在时间为偶数的时候则投一张明确的反对票。
@Override public int vote(Authentication authentication, Object object, Collection collection) { if(LocalDateTime.now().getMinute() % 2 != 0){ return ACCESS_GRANTED; }else{ return ACCESS_DENIED; } }
如何将新的AccessDecisionVoter添加到现有的AccessDecisionManager中?扩展Secured这类基于方法级的注解,单独新建一个Java Config类,然后重写原有框架中初始化AccessDecisionManager的方法:
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true) @Configuration public class MethodSecurityConfiguration extends GlobalMethodSecurityConfiguration { @Override protected AccessDecisionManager accessDecisionManager() { AffirmativeBased affirmativeBased = (AffirmativeBased) super.accessDecisionManager(); affirmativeBased.getDecisionVoters().add(new MinuteBasedVoter()); return affirmativeBased; } }
浙公网安备 33010602011771号