如何使用Spring Boot连接外部/在线LDAP服务器?

问题描述:

我想在我的Spring Boot应用程序中集成基于LDAP的登录。如何使用Spring Boot连接外部/在线LDAP服务器?

作为第一步,我试图使用这个LDAP服务器(http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/)。

但是,我无法成功连接到服务器,并得到此错误。

nested exception is javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials] 

我在配置类中使用这些信息。

authenticationManagerBuilder.ldapAuthentication() 
      .contextSource().url("ldap://ldap.forumsys.com:389/dc=example,dc=com") 
      .managerDn("cn=read-only-admin,dc=example,dc=com").managerPassword("password") 
      .and() 
      .userSearchBase("ou=mathematicians") 
      .groupSearchBase("ou=mathematicians") 
      .userSearchFilter("(cn={0})"); 

这是我的这个项目的application.properties文件。

spring.ldap.urls=ldap.forumsys.com:389 
spring.ldap.base=cn=read-only-admin,dc=example,dc=com 
spring.ldap.password=password 

任何人都可以使用LDAP服务器为Spring Boot应用程序提供工作配置吗?

因为我从LDAP服务器得到这个错误代码。

LDAP: error code 49 - Invalid Credentials 

该问题与我发送到LDAP服务器以打开通信通道的信息有关。所以当我将我的请求改为另一个对象时,它开始工作。

这是我们需要从Spring发送到LDAP服务器的正确请求。

authenticationManagerBuilder 
       .ldapAuthentication() 
       .userDetailsContextMapper(inetOrgPersonContextMapper()) 
       .userSearchFilter("(uid={0})") 
       .userSearchBase("dc=example,dc=com") 
       .groupSearchBase("ou=mathematicians,dc=example,dc=com") 
       .groupSearchFilter("cn={0}") 
       .contextSource() 
       .url("ldap://ldap.forumsys.com") 
       .port(389) 
       .managerDn("cn=read-only-admin,dc=example,dc=com") 
       .managerPassword("password"); 

最后。经过一番尝试,我已经能够解决这个问题。我会在完成我的工作后发布一些细节。

使用以下应用程序属性。

ldap.enabled = true 

####### LDAP TEST############## 
ldap.urls= ldap://ldap.forumsys.com:389/ 
ldap.base.dn= dc=example,dc=com 
ldap.username= cn=read-only-admin,dc=example,dc=com 
ldap.password= password 
ldap.user.dn.pattern = uid={0}