为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

撒科打诨 2022-06-15 17:51:57
我必须从 LDAP 服务器读取操作属性(createTimeStamp、entryUUID 等)。我试图实现自己的 UserDetailContextMapper 但没有成功。目前我的代码看起来像这样Overridepublic void configure(AuthenticationManagerBuilder auth) throws Exception {    auth            .ldapAuthentication()            .userSearchFilter("uid={0}")            .groupSearchBase("ou=users")            .userDetailsContextMapper(userContextMapper())            .contextSource(contextSource());}@Beanpublic LdapContextSource contextSource() {    LdapContextSource contextSource = new LdapContextSource();    contextSource.setUrl("ldap://ldap.company.pl:389");    contextSource.setBase("dc=company,dc=com");    contextSource.afterPropertiesSet();    return contextSource;}@Beanpublic UserDetailsContextMapper userContextMapper() {    return new CustomUserDetailContextMapper();}我的 UserDetailContextMapper 实现:public class CustomUserDetailContextMapper implements UserDetailsContextMapper {@Overridepublic 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;}@Overridepublic void mapUserToContext(UserDetails user, DirContextAdapter ctx) {    //default impl}}我试过了Attributes attributes = ctx.getAttributes(username, new String[] {"*", "+"});在方法中,但如果我通过了mapUserFromContext(),我就会收到。NamingExceptionusername我正在考虑实现 ldapTemplate.lookup(),但我不确定在哪里实现它,我尝试用 ldapTemplate 做 userRepo 类,但我得到NameNotFoundException了我通过的任何 DN。
查看完整描述

1 回答

?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

  ctx.getAttributes(username, new String[] {"*", "+"});

用户名 - 根据文档,这应该是 DN 这就是 LDAP 知道您从哪个记录中提取数据的方式


第二个参数,如果设置为null,它将检索所有属性


我相信您的错误被抛出,因为您传递的是用户名而不是专有名称。


    /**

     * Retrieves selected attributes associated with a named object.

     * See {@link #getAttributes(Name, String[])} for details.

     *

     * @param name

     *          The name of the object from which to retrieve attributes

     * @param attrIds

     *          the identifiers of the attributes to retrieve.

     *          null indicates that all attributes should be retrieved;

     *          an empty array indicates that none should be retrieved.

     * @return  the requested attributes; never null

     *

     * @throws  NamingException if a naming exception is encountered

     */

    public Attributes getAttributes(String name, String[] attrIds)

            throws NamingException;


查看完整回答
反对 回复 2022-06-15
  • 1 回答
  • 0 关注
  • 133 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信