采用此处提供的建议,我实现了自己的 RoleVoter 类,它扩展了 RoleVoter,我需要添加的额外检查是用户、角色和组织都根据我存储在会话中的组织排列。
我有以下 UserRole 类:
class UserRole implements Serializable {
User user
Role role
Organization organization
....
}
这是我的 OrganizationRoleVoter 类:
class OrganizationRoleVoter extends RoleVoter {
@Override
public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) {
int result = ACCESS_ABSTAIN
Collection<? extends GrantedAuthority> authorities = extractAuthorities(authentication)
attributes.each {ConfigAttribute attribute ->
if (this.supports(attribute)) {
result = ACCESS_DENIED
authorities.each {GrantedAuthority authority ->
//TODO this should also check the chosen organization
if (attribute.attribute.equals(authority.authority)) {
return ACCESS_GRANTED
}
}
}
}
return result
}
Collection<? extends GrantedAuthority> extractAuthorities(Authentication authentication) {
return authentication.getAuthorities();
}
}
正如您在我的 TODO 中看到的那样,这也是我需要说的“这里授予的权限也符合我在会议中放置的组织。真的不知道如何实现这一点。