【问题标题】:Spring LDAP not working with LDAP templateSpring LDAP 不适用于 LDAP 模板
【发布时间】:2020-12-21 18:01:33
【问题描述】:

我正在尝试使用 spring 连接到 LDAP 服务器
有关 LDAP 服务器的可用信息如下:

  1. 主机/ip
  2. 端口
  3. 域 => ou=x,dc=y,dc=z
    我没有关于过滤方法的任何信息,例如与用户名匹配的 uid = {0} 或 cn = {0}

这是我的代码

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.DefaultDirObjectFactory;
import org.springframework.ldap.core.support.LdapContextSource;

@Configuration
public class LdapConfiguration {
    
  @Bean
  public LdapContextSource contextSource() {
      LdapContextSource contextSource = new LdapContextSource();
      contextSource.setUrl("ldap://ip:port");
      contextSource.setBase("dc=y,dc=z");
      return contextSource;
  }
    
    @Bean
    public LdapTemplate ldapTemplate() {
        LdapTemplate template = new LdapTemplate(contextSource());
    
        return template;
    }
}

那么这里在另外一个类中就是认证方法

@Service
public class LdapUserServiceImpl implements LdapUserService, BaseLdapNameAware {

  @Autowired
  protected LdapTemplate ldapTemplate;
  
  @Autowired
  protected ContextSource contextSource;

  @Override
  public Boolean authenticate(String userDn, String credentials) {

      AndFilter filter = new AndFilter();
      filter.and(new EqualsFilter("uid", userDn));
      boolean authenticated = ldapTemplate.authenticate(LdapUtils.emptyLdapName(), filter.toString(), credentials);
      
      return authenticated;
   }
}
      

我收到以下错误:

m.m.a.ExceptionHandlerExceptionResolver:已解决 [org.springframework.ldap.UncategorizedLdapException:未分类 LDAP 处理期间发生异常;嵌套异常是 javax.naming.NamingException:[LDAP:错误代码 1 - 000004DC:LdapErr: DSID-0C090A7D,注释:为了执行此操作 成功绑定必须在连接上完成。,数据0,v3839

我的问题是这个错误的原因是什么,如果没有模式被称为 uid={0} 或者它是标准的,我该怎么办
另外,我尝试输入 ContextSource 初始化用户名和密码,尽管我认为它们不可用

  contextSource.setUserDn("uid=username,ou=x,dc=y,dc=z"); 
  contextSource.setPassword("password");

这给了我以下错误:

[nio-8005-exec-5] .m.m.a.ExceptionHandlerExceptionResolver:已解决 [org.springframework.ldap.AuthenticationException:[LDAP:错误代码 49 - 80090308:LdapErr:DSID-0C090453,评论:AcceptSecurityContext 错误,数据 52e,v3839

在application.properties文件中,我放了

spring.ldap.embedded.base-dn=dc=y,dc=z
spring.ldap.embedded.port=port

【问题讨论】:

    标签: java spring ldap


    【解决方案1】:

    将 userDn 设置为 username@domainname.com。 contextSource.setUserDn("uid=username,ou=x,dc=y,dc=z"); 对我来说,ou=x 看起来是额外的,下面的语句应该使它起作用。 contextSource.setUserDn("uid=username,dc=y,dc=z");

    问候 阿比

    【讨论】:

    • 非常感谢,但我的问题是我没有在配置类中设置 UserDn 的初始用户名
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 2018-11-17
    相关资源
    最近更新 更多