使用Java连接AD进行账号验证
?
public class LdapHelper {
private static DirContext ctx;
?
? ? public static DirContext getCtx() {
? ? ? ? if (ctx != null ) {
? ? ? ? ? ? return ctx;
? ? ? ? }
? ? ? ? String account = "zhanghao"; ? ? ? ? ? //设置访问账号
? ? ? ? String password = "mima"; ? ?//设置账号密码
? ? ? ? String root = "DC=corp,DC=homelink,DC=com,DC=cn"; // root
? ? ? ? Hashtable<String,String> env = new Hashtable<String,String>();
? ? ? ? env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
? ? ? ? env.put(Context.PROVIDER_URL, "ldap://172.1.1.1:389/" + root);
? ? ? ? env.put(Context.SECURITY_AUTHENTICATION, "simple");
? ? ? ? env.put(Context.SECURITY_PRINCIPAL, account );
? ? ? ? env.put(Context.SECURITY_CREDENTIALS, password);
? ? ? ? try {
? ? ? ? ? ? ctx = new InitialDirContext(env);
? ? ? ? } catch (javax.naming.AuthenticationException e) {
? ? ? ? Logger.getLogger(LdapHelper.class.getName()).log(Level.ERROR, "连接AD服务器账户错误", e);
? ? ? ? } catch (Exception e) {
? ? ? ? Logger.getLogger(LdapHelper.class.getName()).log(Level.ERROR, "连接AD服务器错误", e);
? ? ? ? }
? ? ? ? return ctx;
? ? }
?
? ? public static void closeCtx(){
? ? ? ? try {
? ? ? ? ? ? ctx.close();
? ? ? ? } catch (NamingException ex) {
? ? ? ? ? ? Logger.getLogger(LdapHelper.class.getName()).log(Level.ALL, "关闭AD连接错误", ex);
? ? ? ? }
? ? }
?
?
? ? /**
? ? ?* 验证账户在AD是否生效
? ? ?* @param sysID
? ? ?* @param userCode
? ? ?* @return 0 没有验证 。 1 没有这个数据。 2 数据错误。 3 成功。 4 账号错误。
? ? ?* @throws Exception
? ? ?*/
? ? public static int valAccout(String sysID,String userCode) {
? ? int ret = 0;?
? ? if (sysID != null && !"".equals(sysID) && userCode != null && !"".equals(userCode)){
? ? SearchControls constraints = new SearchControls(); ?
? ? constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
? ? String query = "(&(objectClass=*)(pager=" + sysID + "))";
? ? try{
? ? NamingEnumeration<SearchResult> en = ctx.search("", query, constraints);
? ? if(en == null){ ?
? ? Logger.getLogger(LdapHelper.class.getName()).log(Level.ALL, "系统号"+sysID+"的账号在AD服务器中没有查找到数据", null);
? ? return 1;
? ? } ?
? ? if(!en.hasMoreElements()){ ?
? ? Logger.getLogger(LdapHelper.class.getName()).log(Level.ALL, "系统号"+sysID+"的账号在AD服务器中发生错误", null);
? ? return 2;
? ? }?
? ? while (en != null && en.hasMoreElements()){//maybe more than one element ?
? ? ? ? ? ? ? ?Object obj = en.nextElement();
? ? ? ? ? ? ? ?if(obj instanceof SearchResult){ ?
? ? ? ? ? ? ? ? ? SearchResult entry ?= (SearchResult) obj; ?
? ? ? ? ? ? ? ? ? Attributes attr = entry.getAttributes();
? ? ? ? ? ? ? ? ? Attribute ?att = attr.get("sAMAccountName");
? ? ? ? ? ? ? ? ? String value = att.get().toString();
? ? ? ? ? ? ? ? ? if(value != null ){
? ? ? ? ? ? ? ? ? if(userCode.equals(value)){
? ? ? ? ? ? ? ? ? ret = 3;
? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ret = 4;
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ?} ??
? ? } ?
? ? }catch(Exception e){
? ? Logger.getLogger(LdapHelper.class.getName()).log(Level.ALL, "系统号"+sysID+"的账号在做AD验证时发生错误,错误编号为"+ ret, e);
? ? ret = 2;
? ? }
? ? }
? ? return ret;
? ? }
?
? ? /**
? ? ?* 验证AD账户
? ? ?* @param systemID 系统号
? ? ?* @param userCode 用户名
? ? ?* @return 0 没有验证 。 1 没有这个数据。 2 数据错误。 3 成功。 4 账号错误。
? ? ?*/
? ? public static int valUser(String systemID,String userCode) {
? ? int ret = 0;
? ? if (systemID != null && !"".equals(systemID) && userCode != null && "".equals(userCode)){
? ? try {
? ? getCtx();
ret = valAccout(systemID,userCode);
} catch (Exception e) {
Logger.getLogger(LdapHelper.class.getName()).log(Level.ALL, "AD验证系统号" + systemID + "的用户登录错误,该用户code:" + userCode, null);
}finally{
closeCtx();
}
? ?
? ? }else{
? ? Logger.getLogger(LdapHelper.class.getName()).log(Level.ALL, "获取验证信息系统号" + systemID + "userCode" + userCode, null);
? ? }
? ? return ret;
? ? }
? ? public static void main(String[] args) throws Exception {?
? ? ? ? getCtx();
? ? ? ? valAccout("10500100","nj_wangying");
? ? ? ? closeCtx();
? ? }
?
}