Commit 898acaef by zsluo

对接ldap

parent 2311025b
...@@ -81,56 +81,41 @@ public class ADServiceImpl implements ADService { ...@@ -81,56 +81,41 @@ public class ADServiceImpl implements ADService {
return null; return null;
} }
// 拆分节点列表 String[] nodes = ldapInfo.getHost().split(",");
List<String> nodeList = Arrays.stream(ldapInfo.getHost().split(","))
.map(String::trim)
.filter(StringUtils::isNotEmpty)
.toList();
if (nodeList.isEmpty()) {
logger.error("LDAP主机配置为空");
return null;
}
// 全局默认端口
String globalPort = ldapInfo.getPort(); String globalPort = ldapInfo.getPort();
String dn = ldapInfo.getDn();
for (String node : nodeList) { for (String node : nodes) {
String host; String host = node.trim();
String port; String port = globalPort;
// 解析 节点自带端口 或 使用全局端口 if (host.contains(":")) {
if (node.contains(":")) { String[] hp = host.split(":", 2);
String[] hp = node.split(":", 2);
host = hp[0].trim(); host = hp[0].trim();
port = hp[1].trim(); port = hp[1].trim();
} else {
host = node;
port = globalPort;
} }
Hashtable<String, String> env = new Hashtable<>();
String ldapUrl = "ldap://" + host + ":" + port; String ldapUrl = "ldap://" + host + ":" + port;
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapUrl); env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_PRINCIPAL, bindUser); env.put(Context.SECURITY_PRINCIPAL, bindUser);
env.put(Context.SECURITY_CREDENTIALS, bindPwd); env.put(Context.SECURITY_CREDENTIALS, bindPwd);
env.put("com.sun.jndi.ldap.connect.timeout", DEFAULT_TIME_OUT);
env.put("com.sun.jndi.ldap.connect.timeout", "5000");
env.put("com.sun.jndi.ldap.connect.timeout.net", "5000");
env.put("com.sun.jndi.ldap.read.timeout", "5000"); env.put("com.sun.jndi.ldap.read.timeout", "5000");
try { try {
logger.info("尝试连接AD域节点:{} -> 账号:{}", ldapUrl, bindUser); logger.info("正在尝试连接AD节点: {}", ldapUrl);
LdapContext ctx = new InitialLdapContext(env, null); InitialLdapContext ctx = new InitialLdapContext(env, null);
logger.info("AD域节点连接成功:{}", ldapUrl); logger.info("节点 {} 连接验证成功!", ldapUrl);
return ctx; return ctx;
} catch (Exception e) { } catch (Exception e) {
logger.error("AD域节点连接失败:{},原因:{}", ldapUrl, e.getMessage()); logger.error("节点 {} 无法接通,准备自动切流。原因: {}", ldapUrl, e.getMessage());
} }
} }
logger.error("致命错误:所有配置的AD域控节点全盘失联!");
logger.error("所有AD域节点全部连接失败");
return null; return null;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment