LDAPVerifyUtils.java(LDAP工具类)

本文介绍了一个用于LDAP认证的工具类实现,采用单例模式,包括连接、断开连接及用户验证等功能。通过实例化工具类并提供必要的参数,可以进行用户DN获取及密码验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**LDAP校验工具类--好像用到了单例模式
*/
public class LDAPVerifyUtils {

private static final Logger log =  LoggerFactory.getLogger(LDAPVerifyUtils.class);

private static LDAPVerifyUtils util;
private String URL;           //
private String BASEDN;
private String ROOT;
private String PASSWORD;
private String FACTORY;
private Control[] connCtls = null;
private LdapContext ctx;  //Ldap上下文
//有参构造
private LDAPVerifyUtils (String root ,String password){
	URL = PropertiesUtil.getAppContext("ldap.url");
	BASEDN = PropertiesUtil.getAppContext("ldap.searchbase");
	ROOT = root;
	PASSWORD = password; 
	FACTORY= PropertiesUtil.getAppContext("ldap.ldapCtxFactory");
}
//获取实例
public static LDAPVerifyUtils  getInstance(String root ,String password){
util = new LDAPVerifyUtils(root,password)  ;
return util;
}

//启动连接
private void LDAP_connect(){
	Hashtable<String ,String> env = new Hashtable<String ,String> ();
	env.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY): //javax.naming.Context
	env.put(Context.PROVIDER_URL, URL+BASEDN);
	env.put(Context.SECURITY_PRINCIPLE, "dahuatech\\"+ROOT);
	env.put(Context.SECURITY_CREDENTIALS, PASSWORD);
	env.put(Context.SECURITY_AUTHENTICATION, SIMPLE);
		try{
			ctx = new InitialLdapContext(env, connCtls);
			log.info("连接至LDAP服务器成功! ");
		}catch(javax.naming.AuthenticationException e){
			log.error("连接至LDAP服务器失败"); //LDAP认证失败,用户名或密码错误
			log.error(e.toString());
		}catch(Exception e){
			log.error("连接至LDAP服务器出错! ");
			log.error(e.toString());
		}
}
//关闭连接
public void closeContext(){
	//关闭LdapContext
	if(ctx != null ){
			try{
				ctx.close();
			}catch(NamingException e){
			log.error( e.toString() );
			}
	}
}
//获取用户DN(内方法,被认证方法调用)
private String getUserDN(String username){
	String userDN = "";
		try{
			//1:打开连接
			LDAP_connect();
			SearchControls constraints = new SearchControls(); //查询控制项
			constraints .setSearchScope(SearchControls .SUBTREE_SCOPE);
			//2:从根目录开始查找
			NamingEnumeration<SearchResult> en = ctx.search( "","(SAMAccountName=)"+username+")", constraints );
			if( en == null || !en.hasMoreElements() ){ log.info("未找到该用户! ");} //枚举的判断空 用 hasMoreElements();
			//3:获取用户DN,注意拼接BASEDN
				while( en != null && en.hasMoreElements() ){
						Object obj = en.nextElement();
						if(obj instanceof SearchResult ){
							SearchResult si = (SearchResult) obj;
							userDN += si.getName();
							userDN += "," + BASEDN;
						}else{
						}
				}  
			}catch(Exception e){log.info("查找用户时发生异常...");}
	return userDN;
}

//校验域用户名合法性
public void authenticate(String username, String password)throws Exception{
	try{
	//获取用户DN
	String userDN = getUserDN(username);
	if(userDN == null || "".equals( userDN.trim() )){ throw new Exception("用户名,密码不正确! ") ;}
	ctx.addToEnvironment(Context.SECURITY_PRINCIPLE,userDN);
	ctx.addToEnvironment(Context.SECURITY_CREDENTIALS,password);
	ctx.reconnect(conntls);
	}finally{
		//关闭相关连接
		closeContext();
	}
}

}//类尾

同学公司用的LDAP认证,我们公司也是的,但只是这个项目.学的时候学的是SpringSecurity. oh ,myGOD!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值