首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将用户从LDAP复制到应用数据库以处理应用层的授权

如何将用户从LDAP复制到应用数据库以处理应用层的授权
EN

Stack Overflow用户
提问于 2016-12-27 15:54:21
回答 1查看 696关注 0票数 1

嗨,我不太确定LDAP和spring的安全性。我有一个需求,因为应用程序身份验证必须由LDAP执行,授权机制必须由应用层处理。我使用的是具有spring安全实现的Jhipster。但是,我能够连接到LDAP并对用户进行身份验证。

现在,授权机制必须由应用层来处理,这样我就可以管理权限了。因此,如果用户在用户身份验证过程之后不存在,我想将用户信息从LDAP复制到应用层数据库。那么,如何用spring安全框架来实现这一点呢?如何拦截过滤器链或某些进程来做到这一点。

最后,这是一个好的方法,还是有更好的方法来处理这个问题。

EN

回答 1

Stack Overflow用户

发布于 2016-12-28 04:25:19

这就是我在项目中实现LDAP身份验证和本地授权的方式。

Configuration:

代码语言:javascript
复制
<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>

自定义权威人士:

代码语言:javascript
复制
@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>();
    User user = userDao.searchUser(username);

    List<String> roleList = userDao.getRoles(username);
    if (!roleList.isEmpty()) {
         for (String role : roleList) {
            System.out.println(role);
            authorities.add(new SimpleGrantedAuthority(role));
        }
    } 
    return authorities;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41348211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档