如何从 ldap 服务器读取操作属性

我必须从 LDAP 服务器读取操作属性(createTimeStamp、entryUUID 等)。


我试图实现自己的 UserDetailContextMapper 但没有成功。


目前我的代码看起来像这样


Override

public void configure(AuthenticationManagerBuilder auth) throws Exception {

    auth

            .ldapAuthentication()

            .userSearchFilter("uid={0}")

            .groupSearchBase("ou=users")

            .userDetailsContextMapper(userContextMapper())

            .contextSource(contextSource());

}


@Bean

public LdapContextSource contextSource() {

    LdapContextSource contextSource = new LdapContextSource();

    contextSource.setUrl("ldap://ldap.company.pl:389");

    contextSource.setBase("dc=company,dc=com");

    contextSource.afterPropertiesSet();

    return contextSource;

}


@Bean

public UserDetailsContextMapper userContextMapper() {

    return new CustomUserDetailContextMapper();

}

我的 UserDetailContextMapper 实现:


public class CustomUserDetailContextMapper implements UserDetailsContextMapper {


@Override

public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<?

        extends GrantedAuthority> authorities) {

    AutoUser user = new AutoUser();


    user.setCreateTimeStamp(ctx.getStringAttribute("createTimestamp"));

    user.setUUID(ctx.getStringAttribute("entryUUID"));

    user.setEmail(ctx.getStringAttribute("mail"));

    return user;

}


@Override

public void mapUserToContext(UserDetails user, DirContextAdapter ctx) {

    //default impl

}

}


我试过了Attributes attributes = ctx.getAttributes(username, new String[] {"*", "+"});


在方法中,但如果我通过了mapUserFromContext(),我就会收到。NamingExceptionusername


我正在考虑实现 ldapTemplate.lookup(),但我不确定在哪里实现它,我尝试用 ldapTemplate 做 userRepo 类,但我得到NameNotFoundException了我通过的任何 DN。


撒科打诨
浏览 160回答 1
1回答

鸿蒙传说

&nbsp; ctx.getAttributes(username, new String[] {"*", "+"});用户名 - 根据文档,这应该是 DN 这就是 LDAP 知道您从哪个记录中提取数据的方式第二个参数,如果设置为null,它将检索所有属性我相信您的错误被抛出,因为您传递的是用户名而不是专有名称。&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Retrieves selected attributes associated with a named object.&nbsp; &nbsp; &nbsp;* See {@link #getAttributes(Name, String[])} for details.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param name&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The name of the object from which to retrieve attributes&nbsp; &nbsp; &nbsp;* @param attrIds&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the identifiers of the attributes to retrieve.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null indicates that all attributes should be retrieved;&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; an empty array indicates that none should be retrieved.&nbsp; &nbsp; &nbsp;* @return&nbsp; the requested attributes; never null&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @throws&nbsp; NamingException if a naming exception is encountered&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public Attributes getAttributes(String name, String[] attrIds)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws NamingException;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java