基於密碼的身份驗證方式有兩種:password 和 md5 ;
Password是明文密碼 md5是加密后的密碼
測試方法類似於基於IP的trust reject身份驗證方式,但數據庫連接參數 密碼需要賦值。
首先新建兩個用戶 user2 ,user3
創建用戶及授權的命令
template1=#create user user2 with nosuperuser nocreatedb password '123456a' ;
template1=# create user user3 with nosuperuser nocreatedb encrypted password '123456a' ;
然后管理員登陸數據庫testdb,使得user2,user3可以訪問表test1;
testdb=# GRANT all on table test1 to user2;
testdb=# GRANT all on table test1 to user3;
測試password方式
服務端配置文件
host testdb user2 10.47.0.153/32 password
host testdb user3 10.47.0.153/32 password
客戶端代碼
String url = "jdbc:postgresql://10.110.18.241/testdb";
Connection conn = DriverManager.getConnection(url, "user2", "123456a");
測試結果
User2,user3都連接數據庫成功。
測試md5方式
服務端配置文件
host testdb user2 10.47.0.153/32 md5
host testdb user3 10.47.0.153/32 md5
客戶端代碼
String url = "jdbc:postgresql://10.110.18.241/testdb";
Connection conn = DriverManager.getConnection(url, "user2", "123456a");
測試結果
User2,user3都連接數據庫成功。
分析:
從測試情況看,服務端為數據庫用戶設置為password和md5,客戶端密碼為123456a都是可以的,那么password 和 md5 兩種情況的區別在哪里呢?
抓取了兩種情況時的網絡通信數據包,發現在運行時password網絡傳輸的是明文,而md5方式網絡傳輸的是md5編碼后的密碼。客戶端程序是完全一樣的。Md5避免了從網絡抓包獲取到明文的密碼。
Password
PostgreSQL
Type: Password message
Length: 12
Password: 123456a
Md5
PostgreSQL
Type: Password message
Length: 40
Password: md52a2922ec50be4efc34ba50cc1cc190e2
如果網絡通信使用了SSL,那么直接使用password也是安全的。
從PostgreSQL8.4 中有說明 However, md5 cannot be used with the db_user_namespace Feature 。
