---
title: "使用Apache的mod_authn_dbd和PostgreSQL实施HTTP Basic登录认证"
author: "zamber"
date: "2011-03-26T20:33:00Z"
tags: []
slug: "apache-mod-authn-dbd-postgresql-http-basic"
draft: false
lastmod: "2012-04-24T14:29:11Z"
id: 1496656
url: "https://zamber.iteye.com/blog/1496656"
---

**【STEP1】**

使用PostgreSQL的客户端pgAdmin

登录数据库，做成用户认证表。字段名和表名任意，如

1 CREATE TABLE usrinfo  
2 (  
3   userid character varying NOT NULL,  
4   passwd character varying NOT NULL,  
5   CONSTRAINT usrinfo\_pk PRIMARY KEY (userid)  
6 )  
7 

 

**【STEP2】**

由于使用Apache的mod\_authn\_dbd进行用户登录认证的时候，要求用户密码必须使用密文保存。这里我们使用Apache的commons-codec

来加密密码明文。

1         String inpasswd = "test";  
2         String passwd = "{SHA}" + Base64.encodeBase64String(MessageDigest.getInstance("SHA1").digest(inpasswd.getBytes()));

 

**【STEP3】**

为了之后Apache的mod\_authn\_dbd模块可以找到数据库的驱动，需要将PostgreSQL的驱动添加到LIBARAY\_PATH中。由于操作系统是Redhat Enterprise Server 5，所以采用的方式是，在/etc/ld.so.conf.d目录下建立apr-util-httpd-1.2.conf这个文件，其中添加数据库驱动的路径。

*※    不知道数据库驱动的路径的话，可以使用find命令查找。*

find / -name libpq.so

 

**【STEP4】**

下载Apache Http Server 2.2进行编译、安装。可以在客户端下载后，用sftp传到服务器上，也可以使用wget命令从internet上获取。

tar zxvf httpd-2.2.17.tar.gz

 

**【STEP5】**

为了使用mod\_authn\_dbd模块，首先必须安装apr框架。另外，Apache2.2.x要求apr的版本为1.2，如果服务器上已经存在apr 1.1的话，需要升级为1.2。在Apache2.2.x的configure如果指定了如下参数好像可以直接安装apr，但这次我们是分别安装apr和Apache Http Server。

--enable-dbd --enable-authn-dbd --with-pgsql=/usr/local/pgsql

 

**【STEP6】**

进入httpd-2.2.17/srclib/apr目录，安装apr

框架。

1 ./configure --prefix=/usr/local/apr-httpd/  
2 make  
3 make install

 

**【STEP7】**

安装apr-util 1.2。

1 ./configure --prefix=/usr/local/apr-util-httpd/ --with-apr=/usr/local/apr-httpd/ --with-pgsql=/usr/local/pgsql  
2 make  
3 make install

一般地，

/usr/local/pgsql是PostgreSQL数据库安装的HOME目录。如果不在这个目录下面的话，可以使用find / -name pgsql来查找名为“pgsql”的目录。可能找到若干个，PostgreSQL数据库安装的HOME目录的特征是下面包含data/base和data/global两个目录。

 

**【STEP8】**

安装Apache Http Server。

 

 1 "./configure" \\  
 2 "--prefix=/usr/local/apache22/" \\  
 3 "--with-apr=/usr/local/apr-httpd/" \\  
 4 "--with-apr-util=/usr/local/apr-util-httpd/" \\  
 5 "--enable-rewrite=shared" \\  
 6 "--enable-ssl" \\  
 7 "--enable-proxy" \\     &lt;-- 因为之后要做到Tomcat的转发，所以加入proxy功能  
 8 "--enable-vhost-alias" \\  
 9 "--enable-dbd"         &lt;-- 认证功能必须  
10 "--enable-authn-dbd"   &lt;-- 认证功能必须  
11 make  
12 make install

 

※可以使用./configure --help来查看支持的参数。 

 

**【STEP9】**

下面就可以启动WEB服务器，看服务器是否可以正常运行。

 

1 cd /usr/local/apache22/bin  
2 ./apachectl start  
3 ps –ef | grep httpd     &lt;-- 查看服务是否启动

使用浏览器访问地址，如果出现“It Works!

”表明已经安装好。

 

**【STEP10】**

停止服务器，修改conf/http.conf文件。

 

./apachectl stop

【httpd.conf】

 1 …  
 2 #Database Management  
 3 #Use the PostgreSQL driver  
 4 DBDriver pgsql  
 5   
 6 #Connection string: database name and login credentials  
 7 DBDParams "host=192.168.0.75 dbname=postgres user=test password=test"  
 8   
 9 #Parameters for Connection Pool Management  
10 DBDMin 1  
11 DBDKeep 2  
12 DBDMax 10  
13 DBDExptime 60  
14   
15 #Authentication Section  
16 &lt;Location "/"&gt;  
17   
18 #mod\_auth configuration for authn\_dbd  
19 AuthType Basic  
20 AuthName "mongoose"  
21 AuthBasicProvider dbd  
22   
23 #authz configuration  
24 Require valid-user  
25   
26 #SQL query to verify a user  
27 #(note: DBD drivers recognise both stdio-like %s and native syntax)  
28 AuthDBDUserPWQuery "select passwd from userinfo where userid = %s"  
29   
30 &lt;/Location&gt;  
31 ![](http://www.blogjava.net/Images/dot.gif)

 

**【STEP11】**

再次启动服务器，访问WEB

服务器后，就会弹出要求登录的对话框，输入数据库中保存的用户名和密码后，应该可以正常登录了。

**【STEP12】**

之后为了连接Tomcat，在http.conf

中加入

ProxyPass / ajp://localhost:8009/

此时，所有对Apache Http Server的请求都会转发给Tomcat，而Apache Http Server就作为一个前端认证的服务器使用。另外，在Tomcat的JSP中，使用request.getRemoteUser还可以取到登录的用户名。