Springboot : 连接ldap超时问题

发布时间 2023-07-04 17:13:41作者: 希望能摸鱼的凛耶酱

Err: java.net.ConnectException: Connection timed out when connecting to ldap

使用springboot ldap 连接账号所属ldap目录验证时,出现如上报错
经检查,host,username,password 等信息均无误,如下为代码中的配置信息示例

hashEnv.put(Context.SECURITY_AUTHENTICATION,"simple");
hashEnv.put(Context.SECURITY_PRINCIPAL,username@domain);
hashEnv.put(Context.SECURITY_CREDENTIALS,password);
hashEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
hashEnv.put(Context.PROVIDER_URL,"ldap://host:389");

尝试使用python 进行ladp验证,等待2分钟后,可获得ldap 信息
通过 码农家园同问题中的solution: 关于spring:java.net.ConnectException:连接到ldap时连接超时 ,可解决并连接成功。

Answer:
问题是由于DNS中不存在域控制器导致的。
在后台配置时使用的时域名不是IP,执行NSLOOPUP显示该域指向所有IP(linux/powershell command):

NSLOOKUP host

之后查找所有IP在端口389上是否可访问,找到可访问IP后,url更改为"ladp://ip:389"后连接成功

另,之后再更改为host:389后无报错,可能确实只是timeout问题