|
发表于 2021-12-13 20:58:33
|
显示全部楼层
我感觉到我这个方法还没有跑到, 所以问题可能是更之前,要是官方出一个详细的设置指南就好了。
public class LdapTools {
private static Logger logger = LoggerFactory.getLogger(LdapTools.class);
public static boolean auth(String uid, String password){
boolean result = false;
DirContext ctx = null;
try {
String userName = Config.token().getLdapAuth().getUserDn();
userName = StringUtils.replace(userName, "*", uid);
String ldapUrl = Config.token().getLdapAuth().getLdapUrl();
if(!ldapUrl.endsWith("/")){
ldapUrl = ldapUrl+"/";
}
ldapUrl = ldapUrl + Config.token().getLdapAuth().getBaseDn();
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");//设置连接LDAP的实现工厂
env.put(Context.PROVIDER_URL, ldapUrl);// 指定LDAP服务器的主机名和端口号
env.put(Context.SECURITY_AUTHENTICATION, "simple");//给环境提供认证方法,有SIMPLE、SSL/TLS和SASL
env.put("com.sun.jndi.ldap.connect.timeout", "3000");//连接超时设置为3秒
env.put(Context.SECURITY_PRINCIPAL, userName);//指定进入的目录识别名DN
env.put(Context.SECURITY_CREDENTIALS, password); //进入的目录密码
//env.put("filter", filter);
ctx = new InitialDirContext(env);
result = true;
} catch (AuthenticationException e) {
logger.debug("ldap认证失败");
} catch (Exception e) {
logger.error(e);
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (Exception e) {
logger.error(e);
}
}
}
return result;
}
}
|
|