PostgreSQL用户登录失败自动锁定的处理方案( 二 )

  
loop  
–锁定用户  
EXECUTE format(‘alter user %I nologin’,res);   
–断开当前被锁定用户会话  
EXECUTE ‘select pg_catalog.pg_terminate_backend(pid) from pg_catalog.pg_stat_activity where usename=$1’ using res;   
raise notice ‘Account % is locked!’,res;  
end loop;  
end;  
$$ language plpgsql strict security definer set search_path to ‘public’;  
   
测试使用篇  
创建测试用户 。  
1create user test1 encrypted password ‘XXX’;  
模拟test1用户登录失败,输入错误密码 。  
$ psql -h192.168.137.11 -Utest1 postgres  
Password for user test1:   
psql: error: FATAL: password authentication failed for user “test1”  
   
通过外部表查看登录失败的日志 。  
1select * from postgres_log where command_tag=’authentication’ and error_severity= ‘FATAL’;  
可以看到1条数据,手工插入一条登录失败的信息到t_login表 。  
insert into t_login select log_time,user_name,0  
 from postgres_log   
 where command_tag=’authentication’   
 and error_severity= ‘FATAL’;  
   
参考上面登录失败测试,接着再测试2次 。  
然后使用postgres用户登录数据库,观察t_login表数据 。  
postgres=# select * from t_login;  
  login_time  | user_name | flag   
————————-+———–+——  
 2021-02-08 06:24:47.101 | test1  | 0  
 2021-02-08 06:25:16.581 | test1  | 1  
 2021-02-08 06:25:18.429 | test1  | 1  
(3 rows)  
   
再测试两次失败登录,然后使用postgres用户登录数据库,看到提示该用户被锁定 。  
[postgres@node11 ~]$ psql  
NOTICE: Account test1 is locked!  
psql (12.5)  
Type “help” for help.  
postgres=# select * from t_login;  
  login_time  | user_name | flag   
————————-+———–+——  
 2021-02-08 06:45:38.017 | test1  | 0  
 2021-02-08 06:45:58.809 | test1  | 1  
 2021-02-08 06:45:58.809 | test1  | 1  
 2021-02-08 06:46:08.116 | test1  | 1  
 2021-02-08 06:46:11.986 | test1  | 1  
(5 rows)  
   
解锁用户 。  
1update t_login set flag = 0 where user_name=’test1′ and flag=1;  
总结  
session_exec通过用户登录成功后调用login函数去实现锁定登录失败次数过多的用户 。  
此种方式有点繁琐且会造成数据库连接变慢 。  
不支持自动解锁,需要管理用户手工处理 。  
文章来源:脚本之家  
来源地址:https://www.jb51.net/article/208017.htm  
【PostgreSQL用户登录失败自动锁定的处理方案】申请创业报道,分享创业好点子 。,共同探讨创业新机遇!  


特别声明:本站内容均来自网友提供或互联网,仅供参考,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。