查看完整版本: Apache与MySQL整合实现基本身份认证

lily 2007-12-12 20:12

Apache与MySQL整合实现基本身份认证

             <p><P>  <a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, 'Apache');" target="_self"><u><strong>Apache</strong></u></a>来实现基本的用户身份认证有很多种方式,比如最常见的txt文本和DBM格式,但在负载很重的server上-这些都不是理想的<a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, '%B7%BD%B7%A8');" target="_self"><u><strong>方法</strong></u></a>,文本的形式是基于平面的,性能很差而且也不安全;DBM好些但在千或万级用户时还是力不从心,于是用database做后台存储则是很好的方法-比平面搜索更有效而且安全,用户口令以DES加密形式存储在<a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, '%CA%FD%BE%DD%BF%E2');" target="_self"><u><strong>数据库</strong></u></a>的表中。</P><P>  这种实现要归功于Apache本身出色的模块化结构--以及开放的DSO方式,可以使开发人员完成大量的第三方模块,并扩充Apache的功能。我在本文中只写了用<a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, 'Mysql');" target="_self"><u><strong>Mysql</strong></u></a>做后台存储--此外还可用Postgresql,Oracle等来完成,原理一样-都是用各自的模块。</P><P>  让我们开始吧--先去modules.apache.org找到mod_auth_mysql--会有两个我们要用DSO那个-事实上直接去ftp://ftp.kcilink.com/pub/下一个mod_auth_mysql.c.gz就行-好-把它解开是一个mod_auth_mysql.c-好-我们用apxs来生成DSO模块(前题是你用DSO模式编译的Apache)--apxs -c -i -a -L/usr/local/lib/mysql -lmysqlclient &gt;-lm mod_auth_mysql.c即可--这里注意一定要这么写---L/usr/local/lib/mysql是mysql的客户库位置,我假定mysql是用的缺省<a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, '%B0%B2%D7%B0');" target="_self"><u><strong>安装</strong></u></a>)---如果不加在起动Apache时会报错-无法装载此模块。</P><P>  好了看看httpd.conf中应该有LoadModule mysql_auth_module libexec/mod_auth_mysql.so和AddModule mod_auth_mysql.c这两句了,重起Apache也不应该有问题。</P><P>  然后我们进入mysql,mysql&gt;create database auth;</P><P><TABLE style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0><TBODY><TR><TD style="WORD-WRAP: break-word" bgColor=#f3f3f3><FONT face=Verdana>mysql&gt;use auth;<BR>mysql&gt; create table mysql_auth (<BR>-&gt; user_name char(20) not null,<BR>-&gt; user_passwd char(25),<BR>-&gt; groups char(25),<BR>-&gt; primary key (username) );</FONT></TD></TR></TBODY></TABLE></P><P>  注意字段名一定是user_name和user_passwd这个。再插入几条记录:</P><P><TABLE style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0><TBODY><TR><TD style="WORD-WRAP: break-word" bgColor=#f3f3f3><FONT face=Verdana>mysql&gt; insert into mysql_auth values<BR>('xingfei2',encrypt("abcde"),'xingfei');<BR>Query OK, 1 row affected (0.00 sec)<BR>mysql&gt; insert into mysql_auth values<BR>('xingfei',encrypt("abcde"),'xingfei');<BR>Query OK, 1 row affected (0.00 sec)</FONT></TD></TR></TBODY></TABLE></P><P>  这里abcde是口令-用encrypt函数来进行加密,用的是DES算法-这是和unix的password等同的算法-而不是mysql本身加密的password()函数。</P><P>  最后在要保护的目录里建一个.htaccess(别忘了把AllowOverride all打开)内容如下:</P><P><TABLE style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0><TBODY><TR><TD style="WORD-WRAP: break-word" bgColor=#f3f3f3><FONT face=Verdana>authname "xingfei"<BR>authtype basic<BR>AuthMySQLHost localhost ---mysql主机名<BR>authmysqluser root ---mysql用户<BR>authmysqlpassword abc ---mysql用户的口令<BR>AuthMySQLDB auth ---用户所用的库-也就是我们建的库<BR>AuthMySQLUserTable mysql_auth ---所用的表<BR>AuthMySQLGroupField groups ---用户组的字段名<BR>require group xingfei<BR>require user xingfei</FONT></TD></TR></TBODY></TABLE></P><P>  可以把用户都放在一个组里-只要是这个组里的用户即可通过认证,也可require单个或多个用户。</P></p>     <center><input type="image" onclick=copyToClipBoard() src="http://www.phpchina.com/images/phpcn_book_bu_tj.gif" border="0"></center>
页: [1]
查看完整版本: Apache与MySQL整合实现基本身份认证
PageRank