Ldap登陆AD(Active Directory)进行认证的Java示例
原文地址:http://hi.baidu.com/js2007/item/24efbb0fae1c9b90a3df432a
package LdapTest;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.AuthenticationException;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
public class AuthenticationTest { private String BASE_DN ="dc=statestreet,dc=com";
// private DirContext ctx = null;
private LdapContext ctx = null;
private Hashtable env = null;
private Control[] connCtls = null; private void LdapConnect(){
env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=statestreet,dc=com");
env.put(Context.SECURITY_CREDENTIALS, "****"); //自己填入 try{
ctx = new InitialLdapContext(env,connCtls);
// ctx = new InitialDirContext(env);
System.out.println("Login Ldap Server Successful...");
}catch(AuthenticationException e){
System.out.println("Login Ldap Server Failed...");
e.printStackTrace();
}catch(Exception e){
System.out.println("Login Ldap Server Wrong...\n "+e.toString());
}
} private String getUserDN(String userid){
String userDN="";
try{
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration en = ctx.search("","uid="+userid,constraints);
if(en == null){
System.out.println("Have NO such user!");
}
if(!en.hasMoreElements()){
System.out.println("Have NO such user!");
}
while (en != null && en.hasMoreElements()){
Object obj = en.nextElement();
if(obj instanceof SearchResult){
SearchResult si = (SearchResult) obj;
userDN += si.getName();
userDN += "," + BASE_DN;
}
else{
System.out.println(obj);
}
}
}catch(Exception e){
System.out.println("Exception in search user DN : "+e.toString());
}
return userDN;
} public boolean Authenricate(String ID,String pwd){
LdapConnect();
boolean valide = false;
String userDN = getUserDN(ID); if(userDN=="")
return valide; try {
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL,userDN);
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS,pwd);
ctx.reconnect(connCtls);
System.out.println(userDN + " is authenticated! ");
valide = true;
}catch (AuthenticationException e) {
System.out.println(userDN + " is NOT authenticated! ");
valide = false;
}catch (NamingException e) {
System.out.println(userDN + " is NOT authenticated! ");
valide = false;
}
LdapDisconnect();
return valide;
} private void LdapDisconnect(){
if (ctx != null) {
try {
ctx.close();
}
catch (NamingException e) {
e.printStackTrace();
}
}
}
}
Ldap登陆AD(Active Directory)进行认证的Java示例的更多相关文章
- C# AD(Active Directory)域信息同步,组织单位、用户等信息查询
示例准备 打开上一篇文章配置好的AD域控制器 开始菜单-->管理工具-->Active Directory 用户和计算机 新建组织单位和用户 新建层次关系如下: 知识了解 我们要用C# ...
- cmd 执行Dcpromo错误:在该 SKU 上不支持 Active Directory 域服务安装向导,Windows Server 2008 R2 Enterprise 配置AD(Active Directory)域控制器
今天,要安装AD域控制器,运行dcpromo结果提示:在该 SKU 上不支持 Active Directory 域服务安装向导. 以前弄的时候直接就通过了,这次咋回事?终于搞了大半天搞定了. 主要原因 ...
- PHP LDAP class for Active Directory
A class for PHP to talk to Active Directory through LDAP.http://sourceforge.net/projects/adldap/
- 【Azure Developer】使用 adal4j(Azure Active Directory authentication library for Java)如何来获取Token呢
问题描述 使用中国区的Azure,在获取Token时候,参考了 adal4j的代码,在官方文档中,发现了如下的片段代码: ExecutorService service = Executors.new ...
- 配置Nginx使用Active Directory 做认证
配置Nginx使用AD做认证 nginx.conf 配置 http { ldap_server ldap { url ldap://xxx:389/DC=test,DC=com?sAMAccountN ...
- Windows Server 2008 R2 配置AD(Active Directory)域控制器
实施过程: 一.安装Windows Server2008 R2操作系统 (过程略) 二.安装域控制器 1. 修改电脑名称 2.修改电脑DNS 三.配置AD 1.在"服务器管理器"- ...
- Windows Server 2008 R2 配置AD(Active Directory)域控制器 -zhai zi wangluo
http://files.cnblogs.com/zhongweiv/Windows_Server_2008_R2_%E9%85%8D%E7%BD%AEActive_Directory%E5%9F%9 ...
- Java使用LdAP获取AD域用户
随着我们的习大大上台后,国家在网络信息安全方面就有了非常明显的改变!所以如今好多做网络信息安全产品的公司和须要网络信息安全的公司都会提到用AD域server来验证,这里就简单的研究了一下! 先简单的讲 ...
- AD 域服务简介(一)- 基于 LDAP 的 AD 域服务器搭建及其使用(转)
一.前言 1.1 AD 域服务 什么是目录(directory)呢? 日常生活中使用的电话薄内记录着亲朋好友的姓名.电话与地址等数据,它就是 telephone directory(电话目录):计算机 ...
随机推荐
- PAT甲级1049. Counting Ones
PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...
- [原]Redis使用场景及使用经验
Redis is an open source (BSD licensed), in-memory data structure store! 欢迎转载,转载请注明出处 刚刚结束一个游戏类的活动项目, ...
- Python模块之: configobj(转)
原来也有写过一篇文章Python模块之: ConfigParser 用来解析INI文件,但是在使用过程中存在一些问题.比如:1,不能区分大小写.2,重新写入的ini文件不能保留原有INI文件的注释.3 ...
- Fast-settling synchronous-PWM-DAC filter has almost no ripple
An inexpensive way to implement high-resolution digital-to-analog conversion is to combine microcont ...
- How to put a relative path for a DLL statically loaded?
How to put a relative path for a DLL statically loaded? I have a DLL made in Delphi 7/Windows XP tha ...
- Supported_Hardware#4G_LTE_cards_and_modems
https://wiki.mikrotik.com/wiki/Supported_Hardware#4G_LTE_cards_and_modems
- mysql 5.7源码安装
http://blog.itpub.net/29733787/viewspace-1590891/
- Javascript:父类可以调用子类吗?
问:父类可以调用子类吗? 答:可以,经典的模板方法模式就是用的这个特性.
- Appium+python自动化22-Appium Desktop
Appium Desktop 原滋原味的官方文档 Appium Desktop是一款用于Mac.Windows和Linux的开源应用,它提供了Appium自动化服务器在一个漂亮灵活的UI中的强大功能. ...
- 数学图形(2.17)pappus螺线
帕波斯(Pappus of Alexandria) 生于亚历山大,活跃于公元300—350前后.该螺线是一种绕在圆锥上的曲线. #http://www.mathcurve.com/courbes3d/ ...