用户名+密码登录ldap服务器,注意密码没有存储在ldap中的password

用用户名+密码模拟登录ldap服务器,不是从ldap中获取密码比较


public class LdapUserAuthenticate {
	private String URL = "ldap://10.41.83.236:389/";
	private String BASEDN = "dc=zte,dc=intra";
	private String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
	private LdapContext ctx = null;
	private Hashtable<String, String> env=null;
	private Control[] connCtls = null;
	private SearchResult searchResult;

	private void connectToLDAPServer() {
		env = new Hashtable<String, String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
		env.put(Context.PROVIDER_URL, URL + BASEDN);// LDAP server
		env.put(Context.SECURITY_AUTHENTICATION, "simple");
		// 此处若不指定用户名和密码,则自动转换为匿名登录
		env.put(Context.SECURITY_PRINCIPAL,
				"cn=gitlab,ou=NM,ou=Central R&D Institute,ou=R&D Institute,dc=zte,dc=intra");
		env.put(Context.SECURITY_CREDENTIALS, "gitlab");
		try {
			connCtls = new Control[] { new LdapADManagerControl() };
			ctx = new InitialLdapContext(env, connCtls);
		} catch (javax.naming.AuthenticationException e) {
			System.out.println("Authentication faild: " + e.toString());
		} catch (Exception e) {
			System.out.println("Something wrong while authenticating: " + e.toString());
		}
	}

	class LdapADManagerControl implements Control {

		@Override
		public String getID() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean isCritical() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public byte[] getEncodedValue() {
			// TODO Auto-generated method stub
			return null;
		}

	}

	private String getUserDN(String ID) {
		String userDN = "";
		connectToLDAPServer();
		try {
			SearchControls constraints = new SearchControls();
			constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
			NamingEnumeration<?> en = ctx.search("", "sAMAccountName=" + ID, constraints);
			if (en == null) {
				System.out.println("Have no NamingEnumeration.");
			}
			if (!en.hasMoreElements()) {
				System.out.println("Have no element.");
			}
			while (en != null && en.hasMoreElements()) {// maybe more than one														// // element
				Object obj = en.nextElement();
				if (obj instanceof SearchResult) {
					SearchResult si = (SearchResult) obj;
					userDN += si.getName();
					userDN += "," + BASEDN;
					searchResult = si;
				} else {
					System.out.println(obj);
				}
			}
		} catch (Exception e) {
			System.out.println("Exception in search():" + e);
		}
		return userDN;
	}

	public String authenricate(String ID, String password) {
		String username = null;
		if (ID.equals("") || password.equals(""))
			return null;
		else {
			String userDN = "";
			try {
				userDN = getUserDN(ID);
				if (userDN.equals(""))
					return null;
				ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, userDN);
				ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
				ctx.reconnect(connCtls);
				String longName = (String)searchResult.getAttributes().get("CN").get();
				username = longName.split("\\d+")[0];
				return username;
			} catch (AuthenticationException e) {
				System.out.println(userDN + " is not authenticated");
				System.out.println(e.toString());
			} catch (NamingException e) {
				System.out.println(userDN + " is not authenticated");
			}catch (Exception e) {
				System.out.println(userDN + " is not authenticated");
			}
			return null;
		}
	}
}



LdapUserAuthenticate authen = new LdapUserAuthenticate();
username = authen.authenricate(userid,password);
if (username == null) {
	System.out.println("登陆失败");
	response.sendRedirect("login.html");
}else{
	System.out.println("登陆成功");
	CookieUtil.addCookie(response, "userName", username, 1800);
	response.sendRedirect(referer);
}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值