hel*_*ldt 16 spring ldap active-directory spring-security
我正在尝试使用Spring Security 3.1对Active Directory进行身份验证.我得到了认证,一切都很好.
<sec:ldap-server id="ldapServer" url="ldap://ldap/dc=sub,dc=domain,dc=com" port="389" />
<sec:authentication-manager erase-credentials="true" >
<sec:authentication-provider ref="ldapActiveDirectoryAuthProvider" />
</sec:authentication-manager>
<bean id="ldapActiveDirectoryAuthProvider"
class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
<constructor-arg value="domain" />
<constructor-arg value="ldap://server:389/"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
现在回答这个问题.如何处理用户角色以便我可以设置过滤器?
例如.
<sec:intercept-url pattern="/**" access="ROLE_USER"/>
Run Code Online (Sandbox Code Playgroud)
我通过使用UserDetailContextMapper并将我的AD组映射到ROLE_USER,ROLE_ADMIN等,找到了如何执行此操作的方法.
<bean id="ldapActiveDirectoryAuthProvider"
class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
<constructor-arg value="domain" />
<constructor-arg value="ldap://host:389/"/>
<property name="userDetailsContextMapper" ref="tdrUserDetailsContextMapper"/>
<property name="useAuthenticationRequestCredentials" value="true"/>
</bean>
<bean id="tdrUserDetailsContextMapper" class="com.bla.bla.UserDetailsContextMapperImpl"/>
Run Code Online (Sandbox Code Playgroud)
Mapper类:
public class UserDetailsContextMapperImpl implements UserDetailsContextMapper, Serializable{
private static final long serialVersionUID = 3962976258168853954L;
@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authority) {
List<GrantedAuthority> mappedAuthorities = new ArrayList<GrantedAuthority>();
for (GrantedAuthority granted : authority) {
if (granted.getAuthority().equalsIgnoreCase("MY USER GROUP")) {
mappedAuthorities.add(new GrantedAuthority(){
private static final long serialVersionUID = 4356967414267942910L;
@Override
public String getAuthority() {
return "ROLE_USER";
}
});
} else if(granted.getAuthority().equalsIgnoreCase("MY ADMIN GROUP")) {
mappedAuthorities.add(new GrantedAuthority() {
private static final long serialVersionUID = -5167156646226168080L;
@Override
public String getAuthority() {
return "ROLE_ADMIN";
}
});
}
}
return new User(username, "", true, true, true, true, mappedAuthorities);
}
@Override
public void mapUserToContext(UserDetails arg0, DirContextAdapter arg1) {
}
}
Run Code Online (Sandbox Code Playgroud)
Sha*_*eep 15
您还可以注入GrantedAuthoritiesMapper3.1中引入的作为修改权威的一般策略.另外,您可能希望SimpleGrantedAuthority用于GrantedAuthority实施.或者,您可以使用枚举,因为您有一组固定的值:
enum MyAuthority implements GrantedAuthority {
ROLE_ADMIN,
ROLE_USER;
public String getAuthority() {
return name();
}
}
class MyAuthoritiesMapper implements GrantedAuthoritiesMapper {
public Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities) {
Set<MyAuthority> roles = EnumSet.noneOf(MyAuthority.class);
for (GrantedAuthority a: authorities) {
if ("MY ADMIN GROUP".equals(a.getAuthority())) {
roles.add(MyAuthority.ROLE_ADMIN);
} else if ("MY USER GROUP".equals(a.getAuthority())) {
roles.add(MyAuthority.ROLE_USER);
}
}
return roles;
}
}
Run Code Online (Sandbox Code Playgroud)
beans.xml 中的角色必须与memberOf 值属性的CN(通用名称)完全匹配。您应该阅读有关目录基础知识的教程。
假设有这个用户:
CN=Michael-O,OU=Users,OU=department,DC=sub,DC=company,DC=net
在他的上下文中存在这个 memberOf 值CN=Group Name,OU=Permissions,OU=Groups,OU=department,DC=sub,DC=company,DC=net
Bean 将找到该 memberOf 值并提取Group Name。beans.xml 必须具有这个值。
| 归档时间: |
|
| 查看次数: |
23363 次 |
| 最近记录: |