【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
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()));
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
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
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 " \ <-- 因为之后要做到Tomcat的转发,所以加入proxy功能
8 " --enable-vhost-alias " \
9 " --enable-dbd " <-- 认证功能必须
10 " --enable-authn-dbd " <-- 认证功能必须
11 make
12 make install
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 " \ <-- 因为之后要做到Tomcat的转发,所以加入proxy功能
8 " --enable-vhost-alias " \
9 " --enable-dbd " <-- 认证功能必须
10 " --enable-authn-dbd " <-- 认证功能必须
11 make
12 make install
※可以使用 ./configure --help 来查看支持的参数。
【STEP9】
下面就可以启动WEB服务器,看服务器是否可以正常运行。
1
cd
/
usr
/
local
/
apache22
/
bin
2 ./ apachectl start
3 ps –ef | grep httpd <-- 查看服务是否启动
使用浏览器访问地址,如果出现“
It Works!
”表明已经安装好。2 ./ apachectl start
3 ps –ef | grep httpd <-- 查看服务是否启动
【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 < Location " / " >
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 </ Location >
31
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 < Location " / " >
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 </ Location >
31
【STEP11】
再次启动服务器,访问 WEB 服务器后,就会弹出要求登录的对话框,输入数据库中保存的用户名和密码后,应该可以正常登录了。【STEP12】
之后为了连接 Tomcat ,在 http.conf 中加入
ProxyPass
/
ajp
://
localhost
:
8009
/
此时,所有对 Apache Http Server 的请求都会转发给 Tomcat ,而 Apache Http Server 就作为一个前端认证的服务器使用。另外,在 Tomcat 的 JSP 中,使用 request.getRemoteUser 还可以取到登录的用户名。