- 论坛徽章:
- 2
|
Description :
今天小玩一下 mod_auth_mysql 的網頁認證,採用 MySQL 資料庫做為後端儲存使用者及密碼,使用上比起以往我們使用過的 Apache 中內函的 .htpasswd 更方便管理也更好做整合,只要在原本就裝有 MySQL 及 Apache 的 Server 上加裝 mod_auth_mysql 外掛模組即可運作,在我的 FreeBSD Server 原本就是裝 MySQL4.1 及 Apache2.0,因此我選用 FreeBSD ports 中的 mod_auth_mysql41_ap2 這個版本的 module 來安裝。
Environment :
硬體:i386 PC Intel P3 500
記憶體網卡:512M RAM + Intel 網卡
作業系統:FreeBSD 6.0 Release
Setp 1.
#mysql -u root -p # 進入 MySQL
Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 22 to server version: 4.1.15 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>CREATE DATABASE auth; # 建立一個 auth 的資料庫
Query OK, 1 row affected(0.03 sec)
mysql>USE auth; # 進入 auth 使用這個資料庫
Database changed
下圖是 auth 這個資料庫的 users 資料表之欄位名稱及資料型態:
![]()
mysql>CREATE TABLE users ( # 建立 users 資料表及欄位名稱跟資料型態
->user_name CHAR(30) NOT NULL, ->user_passwd CHAR(20) NOT NULL, ->PRIMARY KEY (user_name) ->);
Query OK, 0 row affected (0.00 sec)
mysql>INSTERT INTO users VALUES (''john', ENCRYPT('55555')); # 新增帳號 john 密碼為 55555
Query OK, 1 row affected (0.00 sec)
mysql>INSTERT INTO users VALUES (''rika', ENCRYPT('3322')); # 新增帳號 rika 密碼為 3322
Query OK, 1 row affected (0.00 sec)
mysql> GRANT SELECT # MySQL 中建立一個新帳號 authuser,並且給予它對 users 這資料表有 SELECT 的權限也就是讀取的權限,密碼為 1234
->ON auth.users ->TO authuser@localhost ->IDENTIFIED BY '1234';
Query OK, 0 rows affected (0.06 sec)
mysql>exit # 離開 mysql
Bye
Setp 2.
#mkdir /usr/local/www/data-dist/mysql # 建立測試認證帳號的網頁目錄
#echo "Test Page" > /usr/local/www/data-dist/mysql/index.html # 建立測試頁的內容網頁 index.html
#cd /usr/ports/www/mod_auth_mysql41_ap2 # 安裝 mysql 的 auth 模組
#make install clean
#vi /usr/local/etc/apache2/httpd.conf # 設定 apache2 於 httpd.conf 中加入以下幾行
LoadModule mysql_auth_module libexec/apache2/mod_auth_mysql.so AuthName "MySQL authenticated zone" # 認證跳出視窗的名稱
AuthType Basic
AuthMySQLUser authuser # MySQL 有權讀取資料表的使用者
AuthMySQLPassword 1234 # MySQL 有權讀取資料表的使用者密碼
AuthMySQLDB auth # 認證資料庫名稱
AuthMySQLUserTable users # 認證資料表名稱
AuthMySQLNameField user_name # 認證資料表的使用者欄位名稱
AuthMySQLPasswordField user_passwd # 認證資料表的使用者密碼欄位名稱
require valid-user
Setp 3.
#apachectl restart # 重新啟動 apache
最後開個瀏覽器進入測試的目錄就會跳出認證視窗,這時你就可以輸入剛剛建立的帳號及密碼即可登入到 http://主機名稱/mysql/index.html 頁面裡
http://主機名稱/mysql/
![]()
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4206/showart_522965.html |
|