【问题标题】:Where and how ROLES are defined when using LDAP and Spring security使用 LDAP 和 Spring 安全性时在何处以及如何定义 ROLES
【发布时间】:2017-05-26 15:28:35
【问题描述】:

我正在使用 Spring Security 并从 LDAP 数据库进行身份验证。身份验证工作正常,但我无法使用角色!

在 spring-security.xml 中有这个标签:

<security:intercept-url pattern="/app/main/admin" access="hasRole('ROLE_USER')"/>

我的问题是,“ROLE_USER”在哪里定义?如何使用户属于特定角色?这是否发生在 LDAP 数据库中?如果是,那我该怎么做?我对 LDAP 知之甚少。它是我定义角色的另一个配置文件吗?

感谢您的帮助。

【问题讨论】:

    标签: security authentication spring-security ldap role-base-authorization


    【解决方案1】:

    我知道我们可以通过两种方式在 LDAP 身份验证后为用户分配角色。

    1. 我们可以根据用户在 LDAP 数据库中的角色将用户划分为不同的组,并将这些组映射到不同的角色。

    请看这里:How to Map AD Groups to User Role Spring Security LDAP

    1. 我们可以使用 LDAP 身份验证对用户进行身份验证,并使用我们的本地数据库进行授权。

    配置

        <beans:bean id="ldapAuthProvider"  class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
        <beans:constructor-arg name="authenticator">
            <beans:bean
                class="org.springframework.security.ldap.authentication.BindAuthenticator">
                <beans:constructor-arg ref="contextSource" />
                <beans:property name="userSearch">
                    <beans:bean
                        class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
                        <beans:constructor-arg name="searchBase"
                            value="ou=example,dc=springframework,dc=org" />
                        <beans:constructor-arg name="searchFilter"
                            value="(uid={0})" />
                        <beans:constructor-arg name="contextSource"
                            ref="contextSource" />
                    </beans:bean>
                </beans:property>
            </beans:bean>
        </beans:constructor-arg>
        <beans:constructor-arg name="authoritiesPopulator"
            ref="myLDAPAuthPopulator" />
    </beans:bean>
    
    
    <authentication-manager alias="authenticationManager">
    <authentication-provider ref="ldapAuthProvider" />
    </authentication-manager>
    

    myLDAPAuthPopulator的实现:

    @Component("myLDAPAuthPopulator")
    public class MyLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator   {
    
    @Autowired
    private UserDao userDao;
    
    @Override
    public Collection<? extends GrantedAuthority> getGrantedAuthorities(
        DirContextOperations userData, String username) {
    
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    
    List<String> roleList = userDao.getRoles(username);
    if (!roleList.isEmpty()) {
         for (String role : roleList) {
            System.out.println(role);
            authorities.add(new SimpleGrantedAuthority(role));
        }
    }
    else
    {
     //We know that user is authenticated. So you can add a role here and save it in the database. 
    }
    return authorities;
    }
    

    【讨论】:

    • amant singh,感谢您的回答。那么,这意味着我应该在应用程序数据库中将用户分配给 ldap 之外的角色吗?每当我们有一个新用户时,他们会在 LDAP 和应用程序数据库中设置吗?会试一试,看看情况如何。再次感谢。
    • ldap 有组,所以我们不应该以某种或其他方式直接使用它们。一个群体应该暗示一个角色。正确的。这将在很大程度上简化授权。不支持这个 - 使用 ldap 组(角色)授权。
    猜你喜欢
    • 2011-09-22
    • 2014-08-07
    • 2016-04-15
    • 2019-12-11
    • 2011-10-30
    • 2017-07-02
    • 2020-04-14
    • 2011-10-23
    • 2019-04-03
    相关资源
    最近更新 更多