在Spring Security中实现分层角色
问题描述:
我试图在Spring安全中实现分层角色,并根据Spring源文档在我的xml文件中添加了以下配置。在Spring Security中实现分层角色
<bean id="roleHierarchy" class="org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl">
<property name="hierarchy">
<value>
ROLE_ADMIN > ROLE_PRO
ROLE_PRO > ROLE_PREMIUM
ROLE_PREMIUM > ROLE_BASIC
ROLE_BASIC > ROLE_ANONYMOUS
</value>
</property>
</bean>
<bean id="roleVoter"
class="org.springframework.security.access.vote.RoleHierarchyVoter">
<constructor-arg ref="roleHierarchy"/>
</bean>
我试图与上述各行,但我正在访问被拒而ROLE_ADMIN试图访问分配给ROLE_BASIC的URL。我需要添加比这更多的东西吗?除了Spring站点中的这些行之外,我什么都没发现。另外,如果您知道任何分层角色的良好实施,请务必提及它们。
答
我想你需要注册roleVoter
在accessDecisionManager
。 @查看this answer举例。
但说实话,我doubt the Spring Hierarchical Voter concept,因为你需要到处添加一个特殊的阶层选民。我个人比较喜欢其他方式:我实现了一个自定义JdbcDaoImpl
,它覆盖了addCustomAuthorities
,并将“正常”角色添加到“现有”一次。
/**
* Extension of {@link JdbcDaoImpl} User Detail Provider, so that is uses the
* {@link PrivilegesService} to extend the provided Authorities.
*
*/
public class JdbcDaoPrivilegesImpl extends JdbcDaoImpl {
private PrivilegesService privilegesService;
public JdbcDaoPrivilegesImpl(final PrivilegesService privilegesService) {
this.privilegesService = privilegesService;
}
@Override
protected void addCustomAuthorities(String username, List<GrantedAuthority> authorities) {
super.addCustomAuthorities(username, authorities);
List<GrantedAuthority> privileges = new ArrayList<GrantedAuthority>();
for (GrantedAuthority role : authorities) {
privileges.addAll(privilegesService.getPrivilegesForRole(role));
}
authorities.addAll(privileges);
}
}
public interface PrivilegesService {
Collection<? extends GrantedAuthority> getPrivilegesForRole(GrantedAuthority role);
}
public class PropertyPrivilegesServiceImpl implements PrivilegesService {
/**
* Property bases mapping of roles to privileges.
* Every role is one line, the privileges are comma separated.
*/
private Properties roleToPrivileges;
public PropertyPrivilegesServiceImpl(Properties roleToPrivileges) {
if (roleToPrivileges == null) {
throw new IllegalArgumentException("roleToPrivileges must not be null");
}
this.roleToPrivileges = roleToPrivileges;
}
@Override
public Collection<? extends GrantedAuthority> getPrivilegesForRole(GrantedAuthority role) {
if (roleToPrivileges == null) {
throw new IllegalArgumentException("role must not be null");
}
String authority = role.getAuthority();
if(authority != null) {
String commaSeparatedPrivileges = roleToPrivileges.getProperty(role.getAuthority());
if (commaSeparatedPrivileges != null) {
List<GrantedAuthority> privileges = new ArrayList<GrantedAuthority>();
for(String privilegeName : StringUtils.commaDelimitedListToSet(commaSeparatedPrivileges)) {
privileges.add(new GrantedAuthorityImpl(privilegeName.trim()));
}
return privileges;
} else {
return Collections.emptyList();
}
} else {
return Collections.emptyList();
}
}
}
实施例配置
<bean id="myUserDetailsService" class="JdbcDaoForUpdatableUsernames">
<constructor-arg ref="propertyPrivilegesService"/>
<property name="dataSource" ref="dataSource"/>
<property name="usersByUsernameQuery" value="SELECT login,encryptedPassword,loginEnabled FROM user WHERE login = ?"/>
<property name="enableAuthorities" value="true"/>
<property name="authoritiesByUsernameQuery" value="SELECT u.login, r.securityRoles FROM user u, user2security_roles r WHERE u.login= ? AND u.id = r. User_fk;"/>
</bean>
<bean id="propertyPrivilegesService" class="PropertyPrivilegesServiceImpl">
<constructor-arg>
<props>
<prop key="ROLE_ADMIN">
ROLE_PREMIUM,
ROLE_BASIC
</prop>
<prop key="ROLE_PREMIUM">
RROLE_BASIC
</prop>
</props>
</constructor-arg>
</bean>
答
尝试通过添加这弹簧的security.xml:
<http auto-config="true" use-expressions="true" access-decision-manager-ref="accessDecisionManager">
<beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
<beans:constructor-arg>
<beans:list>
<beans:ref bean="roleVoter" />
</beans:list>
</beans:constructor-arg>
</beans:bean>
@carlspring:没有可用的公共示例。 (所有你需要在这个答案中实现这个)。 - 名称:我将其命名为“角色特权方法”,但这不是正式名称。 – Ralph