栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

LDAP:如何使用连接详细信息对用户进行身份验证

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LDAP:如何使用连接详细信息对用户进行身份验证

这是我在各个地方发现的东西的混搭。如果您不想使用UnboundID
SDK,它将带您走正确的道路。这不是生产质量,如果您的商店支持,您可能希望在此处添加SSL内容。

public static Boolean validateLogin(String userName, String userPassword) {    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://" + LDAP_SERVER + ":" + LDAP_SERVER_PORT + "/" + LDAP_base_DN);    // To get rid of the PartialResultException when using Active Directory    env.put(Context.REFERRAL, "follow");    // Needed for the Bind (User Authorized to Query the LDAP server)     env.put(Context.SECURITY_AUTHENTICATION, "simple");    env.put(Context.SECURITY_PRINCIPAL, LDAP_BIND_DN);    env.put(Context.SECURITY_CREDENTIALS, LDAP_BIND_PASSWORD);    DirContext ctx;    try {       ctx = new InitialDirContext(env);    } catch (NamingException e) {       throw new RuntimeException(e);    }    NamingEnumeration<SearchResult> results = null;    try {       SearchControls controls = new SearchControls();       controls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Search Entire Subtree       controls.setCountLimit(1);   //Sets the maximum number of entries to be returned as a result of the search       controls.setTimeLimit(5000); // Sets the time limit of these SearchControls in milliseconds       String searchString = "(&(objectCategory=user)(sAMAccountName=" + userName + "))";       results = ctx.search("", searchString, controls);       if (results.hasMore()) {SearchResult result = (SearchResult) results.next();Attributes attrs = result.getAttributes();Attribute dnAttr = attrs.get("distinguishedName");String dn = (String) dnAttr.get();// User Exists, Validate the Passwordenv.put(Context.SECURITY_PRINCIPAL, dn);env.put(Context.SECURITY_CREDENTIALS, userPassword);new InitialDirContext(env); // Exception will be thrown on Invalid casereturn true;       }        else return false;    } catch (AuthenticationException e) { // Invalid Login        return false;    } catch (NameNotFoundException e) { // The base context was not found.        return false;    } catch (SizeLimitExceededException e) {        throw new RuntimeException("LDAP Query Limit Exceeded, adjust the query to bring back less records", e);    } catch (NamingException e) {       throw new RuntimeException(e);    } finally {       if (results != null) {          try { results.close(); } catch (Exception e) {  }       }       if (ctx != null) {          try { ctx.close(); } catch (Exception e) {  }       }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/514996.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号