本文共 6287 字,大约阅读时间需要 20 分钟。
digoal=# create role u1 login encrypted password 'digoal';CREATE ROLEdigoal=# select * from pg_shadow where usename='u1'; usename | usesysid | usecreatedb | usesuper | usecatupd | userepl | passwd | valuntil | useconfig ---------+----------+-------------+----------+-----------+---------+-------------------------------------+----------+----------- u1 | 26383 | f | f | f | f | md5bdf22a6622939c7c7ab2377eaca514d5 | | (1 row)明文存储 :
digoal=# create table pwd_dictionary(pwd text unique);CREATE TABLE创建一个记录用户最后一次修改密码时间的表. 用于实施密码过期提醒策略.
digoal=# create table user_pwd(rolename name not null unique, pwd_modify_time timestamp not null);CREATE TABLE创建用户的函数 :
digoal=# create or replace function create_role(i_rolename name, i_pwd text) returns void as $$declare v_length int := 8;begin -- 密码长度策略 if length(i_pwd) < v_length then raise notice 'password too short, please use password long than %.', v_length; return; end if; -- 密码复杂度策略, 包含数字, 字母大小写. if not(i_pwd ~ '[a-z]' and i_pwd ~ '[A-Z]' and i_pwd ~ '[0-9]') then raise notice 'password too simple, please ensure password contain a-z,A-Z and 0-9.'; return; end if; -- 密码复用策略, 不允许重复使用已经存在的密码. -- 密码字典策略, 不允许使用密码字典中的密码. insert into pwd_dictionary(pwd) values (md5(i_pwd)); -- 插入用户表, 记录用户最后一次修改密码的时间, 用于密码过期策略 insert into user_pwd(rolename, pwd_modify_time) values (i_rolename, now()); -- 创建用户 execute 'create role '||i_rolename||' encrypted password '||quote_literal(i_pwd); raise notice 'create role % successed.', i_rolename; return;end;$$ language plpgsql strict;CREATE FUNCTION创建用户密码过短, 不允许创建.
digoal=# select * from create_role('u4','pwd');NOTICE: password too short, please use password long than 8. create_role ------------- (1 row)创建用户密码过于简单, 不允许创建.
digoal=# select * from create_role('u4','abcdefee');NOTICE: password too simple, please ensure password contain a-z,A-Z and 0-9. create_role ------------- (1 row)创建用户正常.
digoal=# select * from create_role('u4','aA0ffffffff');NOTICE: create role u4 successed. create_role ------------- (1 row)创建用户密码与现有密码重复, 不允许创建.
digoal=# select * from create_role('new','aA0ffffffff');ERROR: duplicate key value violates unique constraint "pwd_dictionary_pwd_key"DETAIL: Key (pwd)=(2b9aa88182d13d35930180b4cc791beb) already exists.CONTEXT: SQL statement "insert into pwd_dictionary(pwd) values (md5(i_pwd))"PL/pgSQL function create_role(name,text) line 17 at SQL statement修改用户密码的函数 :
digoal=# create or replace function alter_role_pwd(i_rolename name, i_pwd text) returns void as $$declare v_length int := 8;begin -- 密码长度策略 if length(i_pwd) < v_length then raise notice 'password too short, please use password long than %.', v_length; return; end if; -- 密码复杂度策略, 包含数字, 字母大小写. if not(i_pwd ~ '[a-z]' and i_pwd ~ '[A-Z]' and i_pwd ~ '[0-9]') then raise notice 'password too simple, please ensure password contain a-z,A-Z and 0-9.'; return; end if; -- 密码复用策略, 不允许重复使用已经存在的密码. -- 密码字典策略, 不允许使用密码字典中的密码. insert into pwd_dictionary(pwd) values (md5(i_pwd)); -- 更新用户表, 记录用户最后一次修改密码的时间, 用于密码过期策略 update user_pwd set pwd_modify_time=now() where rolename=i_rolename; -- 修改用户密码 execute 'alter role '||i_rolename||' encrypted password '||quote_literal(i_pwd); raise notice 'modify role % password successed.', i_rolename; return;end;$$ language plpgsql strict;CREATE FUNCTION使用该函数修改用户密码
digoal=# select * from alter_role_pwd('u4','new');NOTICE: password too short, please use password long than 8. alter_role_pwd ---------------- (1 row)digoal=# select * from alter_role_pwd('u4','new22222222');NOTICE: password too simple, please ensure password contain a-z,A-Z and 0-9. alter_role_pwd ---------------- (1 row)digoal=# select * from alter_role_pwd('u4','new2222222z2');NOTICE: password too simple, please ensure password contain a-z,A-Z and 0-9. alter_role_pwd ---------------- (1 row)digoal=# select * from alter_role_pwd('u4','new2222222z2A');NOTICE: modify role u4 password successed. alter_role_pwd ---------------- (1 row)digoal=# select * from alter_role_pwd('u4','new2222222z2A');ERROR: duplicate key value violates unique constraint "pwd_dictionary_pwd_key"DETAIL: Key (pwd)=(9a5c46207db775d4d98e64d427481cbc) already exists.CONTEXT: SQL statement "insert into pwd_dictionary(pwd) values (md5(i_pwd))"PL/pgSQL function alter_role_pwd(name,text) line 17 at SQL statement
digoal=# select * from user_pwd ; rolename | pwd_modify_time ----------+---------------------------- u4 | 2013-05-25 18:21:59.376404(1 row)
digoal=# set password_encryption=off;SETdigoal=# create role u2 login password 'digoal';CREATE ROLEdigoal=# select * from pg_shadow where usename='u2'; usename | usesysid | usecreatedb | usesuper | usecatupd | userepl | passwd | valuntil | useconfig ---------+----------+-------------+----------+-----------+---------+--------+----------+----------- u2 | 26387 | f | f | f | f | digoal | | (1 row)
转载地址:http://rszma.baihongyu.com/