先查看一下,系统有没有该命令。
whichldapsearch
如果没有的话安装一个
yumlist"*ldap*"
yuminstallopenldap-clients
再查看一下,是否安装好了。
rpm-qlopenldap-clients
whichldapsearch
2. 什么是 php LDAP模块
LDAP(Lightweight Directory Access Protocol)的意思是"轻量级目录访问协议",是一个用于访问"目录服务器"(Directory Servers)的协议。php要能够连接上ldap服务器,必须安装php-ldap模块,并修改php.ini配置,增加配置项extension=ldap.so,之后重启apache服务器。
3. 什么情况适合使用LDAP
今天安装了下wordpress,主要用于团队内部技术交流和分享。它的安装倒是非常简单,修改下数据库配置文件就可以了。但由于团队的账号全部采用LDAP进行管理,因此希望wordpress能与ldap进行集成。 好在wordpress比较成熟,有多种LDAP插件可以使用,在尝试了Simple LDAP Login和Active Directory Integration均告知失败的情况下,只好潜心去看下它的代码,代码倒简单,加上之前写过一个简单的PHP修改LDAP密码的页面,很快就定位问题并圆满解决了。 1、现将问题的修改过程和大家分享,先说下我们LDAP的组织情况,我们在LDAP创建了2个组,一个用户组,一个角色分组,用户分组按照组织架构进行区分,如下图所示: Java代码 +dc=foo,dc=bar,dc=com -+ou=roles -+ou=users --+ou=dev ---+ou=dev09 ----+uid=zhangsan 因此我们base_dn为 dc=foo,dc=bar,dc=com 2、Simple LDAP Login插件修改: a).下载并启用Simple LDAP Login b).在设置项中找到Simple LDAP Login的配置页面,填写Simple选项卡中配置baseDN、LDAP的服务器IP地址,并在Advance配置LDAP Login Attribute为uid。 c).保存后,发现输入正确的用户名和密码后,无法登陆。 d).翻了翻插件的源码才发现在Simple-LDAP-Login.php中ldap_auth方法存在问题。他将我们配置的uid、输入的用户名和配置baseDN链接在一起,即得到的是“uid=zhangsan,dc=foo,dc=bar,dc=com”作为用户的dn查找,而该正确的查找应该为“uid=zhangsan,ou=dev1,ou=dev1,ou=dev,ou=users,dc=foo,dc=bar,dc=com”,因此总是提示我们无法登陆。 Php代码 function ldap_auth( $username, $password, $directory ) { $result = false; if ( $directory == "ad" ) { $result = $this->adldap->authenticate( $username, $password ); } elseif ( $directory == "ol" ) { $this->ldap = ldap_connect( join(' ', (array)$this->get_setting('domain_controllers')), (int)$this->get_setting('ldap_port') ); ldap_set_option($this->ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$this->get_setting('ldap_version')); if ( str_true($this->get_setting('use_tls')) ) { ldap_start_tls($this->ldap); } $ldapbind = @ldap_bind($this->ldap, $this->get_setting('ol_login') .'=' . $username . ',' . $this->get_setting('base_dn'), $password); $result = $ldapbind; } return apply_filters($this->prefix . 'ldap_auth', $result); } e).修改查找部分,先用uid去查找正确的dn,然后用正确的dn和password去绑定,红色部分为修改。 f).最终代码如下: Php代码 function ldap_auth( $username, $password, $directory ) { $result = false; if ( $directory == "ad" ) { $result = $this->adldap->authenticate( $username, $password ); } elseif ( $directory == "ol" ) { $this->ldap = ldap_connect( join(' ', (array)$this->get_setting('domain_controllers')), (int)$this->get_setting('ldap_port') ); ldap_set_option($this->ldap, LDAP_OPT_PROTOCOL_VERSION, (int)$this->get_setting('ldap_version')); if ( str_true($this->get_setting('use_tls')) ) { ldap_start_tls($this->ldap); } //add by gogo1217 $search=@ldap_search($this->ldap,$this->get_setting('base_dn'),$this->get_setting('ol_login') .'=' .$username); $dn=@ldap_get_entries($this->ldap,$search); //从获取到的数组取出用户dn,没有用户dn修改不了密码。 for ($i=0; $i<$dn["count"]; $i++){ $user_dn= $dn[$i]["dn"]; } $ldapbind = @ldap_bind($this->ldap, $user_dn, $password); //end by gogo1217 //$ldapbind = @ldap_bind($this->ldap, $this->get_setting('ol_login') .'=' . $username . ',' . $this->get_setting('base_dn'), $password); $result = $ldapbind; } return apply_filters($this->prefix . 'ldap_auth', $result); } g).LDAP中姓名中文乱码问题: 这是因为插件中对中文进行了转码,去掉转码即可,删除红颜色部分中的sanitize_title函数包裹: 3、Active Directory Integration插件修改: a).下载并启用Active Directory Integration; b).在配置模块中找到Active Directory Integration Settings,填写Server选项卡中的LDAP的服务器IP地址和baseDN。 c).保存后,发现输入正确的用户名和密码后,无法登陆。 d).翻了翻插件的源码,发现使用ad_ldap/adLDAP.php来链接LDAP,他将输入的用户名和我们在User配置页面中用户后缀链接在一起,由于我没有配置后缀即得到的是“zhangsan”作为用户的dn查找,而该正确的查找应该为“uid=zhangsan,ou=dev1,ou=dev1,ou=dev,ou=users,dc=foo,dc=bar,dc=com”,因此总是提示我们无法登陆。 Php代码 /** * Validate a user's login credentials * * @param string $username A user's AD username * @param string $password A user's AD password * @param bool optional $prevent_rebind * @return bool */ public function authenticate($username, $password, $prevent_rebind = false) { // Prevent null binding if ($username === NULL $password === NULL) { return false; } if (emptyempty($username) emptyempty($password)) { return false; } // Bind as the user $ret = true; $this->_bind = @ldap_bind($this->_conn, $username . $this->_account_suffix, $password); if (!$this->_bind){ $ret = false; } e).修改查找部分,先用uid去查找正确的dn,然后用正确的dn和password去绑定,红色部分为修改。 f).最终代码如下: Php代码 /** * Validate a user's login credentials * * @param string $username A user's AD username * @param string $password A user's AD password * @param bool optional $prevent_rebind * @return bool */ public function authenticate($username, $password, $prevent_rebind = false) { // Prevent null binding if ($username === NULL $password === NULL) { return false; } if (emptyempty($username) emptyempty($password)) { return false; } // Bind as the user $ret = true; //add by liushimin $search=@ldap_search($this->_conn,$this->_base_dn,"uid=".$username); $dn=@ldap_get_entries($this->_conn,$search); //从获取到的数组取出用户dn,没有用户dn修改不了密码。 for ($i=0; $i<$dn["count"]; $i++){ $user_dn= $dn[$i]["dn"]; } //end by liushimin $this->_bind = @ldap_bind($this->_conn, $user_dn, $password); //$this->_bind = @ldap_bind($this->_conn, $username . $this->_account_suffix, $password); if (!$this->_bind){ $ret = false; } 4、对比2个插件,都是在用户登陆的时候去LDAP检索,并且发现本地没有用户,则创建一个新的用户并赋予指定的角色。
4. php中,ldap验证的错误: 这个报错始终解决不了,可否相告~
在 LDAP 的协议之中,很像硬盘目录结构或倒过来的树状结构。LDAP 的根就是全世界,第一级是属于国别 (countries) 性质的层级,之后可能会有公司 (organization) 的层级,接着是部门 (organizationalUnit),再来为个人。而就像文件,每个人都会有所谓的显名 (distinguished name, 简称 dn),dn 可能像酱子 cn=John Smith,ou=Accounts,o=My Company,c=US。
<?php
// 本例使用到 connect, bind, search, interpret search
// result, close connection 等等 LDAP 的功能。
echo "<h3>LDAP 搜寻测试</h3>";
echo "连接中 ...";
$ds=ldap_connect("localhost"); // 先连上有效的 LDAP 服务器
echo "连上 ".$ds."<p>";
if ($ds) {
echo "Binding ...";
$r=ldap_bind($ds); // 匿名的 bind,为只读属性
echo "Bind 返回 ".$r."<p>";
echo "Searching for (sn=S*) ..."; // 找寻 S 开头的姓氏
$sr=ldap_search($ds,"o=My Company, c=US", "sn=S*");
echo "Search 返回 ".$sr."<p>";
echo "S 开头的姓氏有 ".ldap_count_entries($ds,$sr)." 个<p>";
echo "取回姓氏资料 ...<p>";
$info = ldap_get_entries($ds, $sr);
echo "资料返回 ".$info["count"]." 笔:<p>";
for ($i=0; $i<$info["count"]; $i++) {
echo "dn 为: ". $info[$i]["dn"] ."<br>";
echo "cn 为: ". $info[$i]["cn"][0] ."<br>";
echo "email 为: ". $info[$i]["mail"][0] ."<p>";
}
echo "关闭链接";
ldap_close($ds);
} else {
echo "<h4>无法连接到 LDAP 服务器</h4>";
}
?>
5. ldap search实现查询manager下管辖的所有员工
ldapsearch 时 base指定为manager的dn,scope指定sub,我想这样就可以了。
6. 回复技术提问中的一个问题:php如何读取域用户信息 - PHP进阶讨论
给你一个完整的示例,我自己搭了个AD服务器测试过代码了。可以运行。这段代码将test.local域中,找到OU为“测试组织单元”中找出所有对象,并且发送成CSV文件的例子。另外,AD外围开发应用不是冷门,很多的保险证券银行等金融机构都会部署AD服务,并且将不同的应用与AD集成。同样一套可以集成到AD的应用和不带有AD的应用在价格不是一点点的差距。通过与AD集成还可以开发各种基SHAREPOINT、Exchange等的应用,还可以利用微软的像CRM等等产品,开发工作流等应用。应该说是很有钱途的。$value) {? ?? ???if (!is_array($value)) { ? ?? ?? ?? ?? ?continue;? ?? ???}? ?? ???echo $value[\'name\'][0], \',\';? ?? ???echo $value[\'mail\'][0], \',\';? ?? ???echo $value[\'mobile\'][0], \',\';? ?? ???echo $value[\'telephonenumber\'][0], \',\'; ? ?? ???echo $value[\'title\'][0], \',\';? ?? ???echo $value[\'physicaldeliveryofficename\'][0], \',\';? ?? ???echo $value[\'company\'][0], \"\\r\\n\";}?>
7. php要支持LDAP需要什么模块
今日需要部署一套ldap的测试环境,但发现现有php在编译的时候忘加上--with-ldap了,遂打算重新编译,但这种方式比较麻烦并且繁琐,如果仅仅是增加一个ldap模块可以通过phpize和configure的方式编译外挂加载ldap的so文件来使php支持ldap扩展拓展。
第一步是找到当前运营环境的php版本,并将此版本的源码包放入运营机器某个目录,解压。
第二步
1 cd /xxx/xxx/php-5.2.17/ext/ldap/
2 /usr/local/php/bin/phpize
3 ./configure --with-php-config=/usr/local/php/bin/php-config --with-ldap
但是检查过程异常退出,提示如下错误:
1 checking for LDAP support... yes, shared
2 checking for LDAP Cyrus SASL support... no
3 checking for 3 arg ldap_set_rebind_proc... yes
4 checking for ldap_parse_result... no
5 checking for ldap_parse_reference... no
6 checking for ldap_start_tls_s... no
7 checking for ldap_bind_s... no
8 configure: error: LDAP build check failed. Please check config.log for more information.
始终验证不通过,这时候排查openldap2-devel版本,以为此版本比较老,就往高版本升级,但是依旧是同样的错误,最终排查觉得有可能是openldap2-client的版本和openldap2-devel版本不一致导致,于是在网上找了一个和client版本完全匹配的devel rpm包,重新configure顺利通过校验。这个问题比较奇怪,通常情况下编译php是不需要开发包和二进制包版本完全一致,但ldap的编译却需要client和devel包版本完全一致。
记得在make ,make install之后在php.ini上将ldap.so写入extension扩展配置中。
8. 我想从LDAP中获取所有的用户信息,用ldapTemplate中的search方法 ,请问应该怎么写,过滤器 filter怎么写
如果你的用户属性里有uid
这可以这样写 (uid=*)
如果没有,可以写任何一个有的属性,然后属性值=*