简体   繁体   English

如何将AD组映射到用户角色Spring Security LDAP

[英]How to Map AD Groups to User Role Spring Security LDAP

I have a web application built using Java Spring MVC. 我有一个使用Java Spring MVC构建的Web应用程序。

I'm just setting up spring security connecting to an LDAP server for authentication. 我只是设置连接到LDAP服务器进行身份验证的spring security。

I've successfully set it up so that I am able to login to my application but I can't find anything to help me in mapping an AD group to a user role within Java as I can only get a 403 forbidden page ie I've been authenticated but don't have permissions yet. 我已成功设置它,以便我能够登录到我的应用程序,但我无法找到任何帮助我将AD组映射到Java中的用户角色,因为我只能获得403禁止页面,即我已经过身份验证但尚未拥有权限。

I currently have: 我目前有:

<http auto-config="true">
    <intercept-url pattern="/**" access="ROLE_USER" />      
</http>

<ldap-server id="ldapServer" url="LDAPURL" manager-dn="USER" manager-password="PASSWORD"  />

<authentication-manager > 
    <ldap-authentication-provider           
        group-search-base="OU=GROUPS"
        group-search-filter="sAMAccountName={0}"

        user-search-base="OU=USERS"
        user-search-filter="sAMAccountName={0}" 

        />
</authentication-manager>

Say that user was a part of the AD group g-group-UK-user I then want to be able to map that AD group to ROLE_USER so that user can then see the whole web app. 假设该用户是AD组g-group-UK-user的一部分,那么我希望能够将该AD组映射到ROLE_USER,以便用户可以看到整个Web应用程序。

I can only seem to find very simple examples where the groups are either ADMIN or USER in which case the prefix ROLE is just added to the group or the other method seems to be using UserDetailContextMapper but I can't find a clear use of this. 我似乎只能找到非常简单的示例,其中组是ADMIN或USER,在这种情况下,前缀ROLE刚刚添加到组中,或者其他方法似乎使用UserDetailContextMapper但我无法清楚地使用它。

To do this I used the following within authentication manager: 为此,我在身份验证管理器中使用了以下内容:

user-context-mapper-ref="customUserContextMapper"

I then used the following class to check if that user belongs to a certain AD group and then assign the ROLE_USER role to their authorities: 然后,我使用以下类检查该用户是否属于某个AD组,然后将ROLE_USER角色分配给其权限:

@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) 
{

    Attributes attributes = ctx.getAttributes();
    Object[] groups = new Object[100];
    groups = ctx.getObjectAttributes("memberOf");

    LOGGER.debug("Attributes: {}", attributes);

    Set<GrantedAuthority> authority = new HashSet<GrantedAuthority>();

    for(Object group: groups)
    {

        if (group.toString().toLowerCase().contains("AD_GROUP_NAME".toLowerCase()) == true)
        {
            authority.add(new SimpleGrantedAuthority("ROLE_USER"));
            break;          
        }
    }

    User userDetails = new User(username, "", false, false, false, false, authority);
    return userDetails;
}

Please note that the class is a little more complicated than usual because of the LDAP server I was connecting which has a different structure than usual in that the groups a user has access to are stored in an attribute under the user and not the other way round in which a group would have as an attribute all the users that belong to it. 请注意,由于我连接的LDAP服务器具有与通常不同的结构,因为用户可以访问的组存储在用户下的属性中而不是相反的方式,因此该类比平时稍微复杂一些。其中一个组将属于它的所有用户作为属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM