以编程方式访问 Spring Security 角色层次结构

Jas*_*son 1 spring spring-security javabeans

我有一个 Spring Security 角色层次结构,设置如下:

ROLE_ADMIN > ROLE_MANAGER
ROLE_MANAGER > ROLE_USER
ROLE_USER > ROLE_GUEST
Run Code Online (Sandbox Code Playgroud)

我发现自己需要创建一个 VetoableChangeListener,它可以根据角色否决 PropertyChangeEvents(由于那些愚蠢的 Legacy Design 问题之一)。

因此,在我的 vetoableChange() 方法中,需要根据层次结构否决更改。例如,某个字段不能被定义的层次结构中 ROLE_MANAGER 之下的任何角色更改,因此如果 ROLE_USER 尝试更改它,则会引发 PropertyVetoException。

public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {

    String role = SecurityContextHolder.getContext().getAuthentication().getRoles().get(0);
    String propertyName = evt.getPropertyName();
    String requiredRole = getRequiredRole(propertyName);

    // determine if the current role is equal to or greater than
    // the required role, throw PropertyVetoException if not

}
Run Code Online (Sandbox Code Playgroud)

任何人都可以提供帮助吗?

pgi*_*cek 5

在您的听众中直接使用RoleHierarchy您定义的。

Collection<? extends GrantedAuthority> roles = Collections.singletonList(new SimpleGrantedAuthority(userRole));
Collection<? extends GrantedAuthority> reachableRoles = roleHierarchy.getReachableGrantedAuthorities(roles);

if (reachableRoles.contains(requiredRole)) {
    // allow
} else {
    // deny
}
Run Code Online (Sandbox Code Playgroud)

方法getReachableGrantedAuthorities(Collection<? extends GrantedAuthority>)返回所有可访问权限的数组。可到达的权限是直接分配的权限加上在角色层次结构中可从它们(可传递地)到达的所有权限。

例子:

角色层次结构:ROLE_A > ROLE_B 和 ROLE_B > ROLE_C。

直接分配的权限:ROLE_A。

可访问权限:ROLE_A、ROLE_B、ROLE_C。