最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PostgreSQL用戶登錄失敗自動鎖定的處理方案

 更新時間:2021年03月21日 08:20:58   作者:寰宇001  
這篇文章主要介紹了PostgreSQL用戶登錄失敗自動鎖定的解決辦法,本文給大家分享解決方案,通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

墨墨導(dǎo)讀:PostgreSQL使用session_exec插件實現(xiàn)用戶密碼驗證失敗幾次后自動鎖定,本文介紹一種處理方案。

一、插件session_exec安裝配置篇

下載插件并編譯安裝。
https://github.com/okbob/session_exec

$ unzip session_exec-master.zip 
$ cd session_exec-master/
$ make pg_config=/opt/pgsql/bin/pg_config
$ make pg_config=/opt/pgsql/bin/pg_config install

配置postgresql.conf。

session_preload_libraries='session_exec'
session_exec.login_name='login'

注意:上面第一個變量是設(shè)置session_preload_libraries而不是通常設(shè)置的shared_preload_libraries。
第二個變量是需要自定義實現(xiàn)的登錄函數(shù)。

重啟數(shù)據(jù)庫服務(wù)。

$ sudo systemctl restart postgresql-12

二、自定義登錄函數(shù)篇

創(chuàng)建t_login表用于存儲提取自數(shù)據(jù)庫日志中登錄失敗的信息。

create table t_login
(
login_time timestamp(3) with time zone --插入時間,
user_name text --數(shù)據(jù)庫登錄用戶,
flag int4 --標志位,0代表過期數(shù)據(jù),1代表正常狀態(tài)數(shù)據(jù)
);

使用file_fdw外部表記錄數(shù)據(jù)庫日志信息。
file_fdw如果未配置過,參見下面步驟。

$ cd /opt/postgresql-12.5/contrib/file_fdw
$ make && make install

create extension file_fdw;
CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;

建立外部表postgres_log,關(guān)聯(lián)數(shù)據(jù)庫日志中登錄失敗的信息。

CREATE FOREIGN TABLE postgres_log( 
 log_time timestamp(3) with time zone, 
 user_name text, 
 database_name text, 
 process_id integer,
 connection_from text,
 session_id text, 
 session_line_num bigint, 
 command_tag text, 
 session_start_time timestamp with time zone, 
 virtual_transaction_id text, 
 transaction_id bigint, 
 error_severity text, 
 sql_state_code text, 
 message text, 
 detail text, 
 hint text, 
 internal_query text, 
 internal_query_pos integer, 
 context text, 
 query text, 
 query_pos integer, 
 location text, 
 application_name text
) SERVER pglog 
OPTIONS ( program 'find /opt/pg_log_5432 -type f -name "*.csv" -mtime -1 -exec cat {} \;', format 'csv' );

注意:
1./opt/pg_log_5432需要修改為實際環(huán)境日志目錄。
2. 不同PG版本csv日志格式可能有所差異,參考PG官網(wǎng)文檔runtime-config-logging章節(jié)(http://postgres.cn/docs/12/runtime-config-logging.html)。

此時連接數(shù)據(jù)庫因未創(chuàng)建登錄函數(shù)會出現(xiàn)下面的警告信息。

$ psql -Upostgres
WARNING: function "login()" does not exist
psql (12.5)
Type "help" for help.

創(chuàng)建登錄函數(shù)login。

create or replace function login() returns void as $$
declare
res text;
c1 timestamp(3) with time zone;
begin

--獲取當前日志中最新時間
select login_time 
from public.t_login 
where flag = 0 
order by login_time 
desc limit 1 
into c1; 

 --將最新的數(shù)據(jù)插入t_login表
insert into public.t_login 
select log_time,user_name 
from public.postgres_log 
where command_tag='authentication' 
and error_severity= 'FATAL' 
and log_time > c1;

update public.t_login set flag = 1 where login_time > c1; 

--檢查登錄失敗次數(shù)是否大于3,若大于3則鎖定用戶
for res in select user_name from public.t_login where flag = 1 group by user_name having count(*) >=3 
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';

測試使用篇

創(chuàng)建測試用戶。

create 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"

通過外部表查看登錄失敗的日志。

select * from postgres_log where command_tag='authentication' and error_severity= 'FATAL';

可以看到1條數(shù)據(jù),手工插入一條登錄失敗的信息到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用戶登錄數(shù)據(jù)庫,觀察t_login表數(shù)據(jù)。

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用戶登錄數(shù)據(jù)庫,看到提示該用戶被鎖定。

[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)

解鎖用戶。

update t_login set flag = 0 where user_name='test1' and flag=1;

總結(jié)

  • session_exec通過用戶登錄成功后調(diào)用login函數(shù)去實現(xiàn)鎖定登錄失敗次數(shù)過多的用戶。
  • 此種方式有點繁瑣且會造成數(shù)據(jù)庫連接變慢。
  • 不支持自動解鎖,需要管理用戶手工處理。

參考鏈接:

http://m.fzitv.net/article/208018.htm

到此這篇關(guān)于PostgreSQL用戶登錄失敗自動鎖定的解決辦法的文章就介紹到這了,更多相關(guān)PostgreSQL登錄失敗自動鎖定內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Linux CentOS 7安裝PostgreSQL9.3圖文教程

    Linux CentOS 7安裝PostgreSQL9.3圖文教程

    這篇文章主要為大家詳細介紹了Linux CentOS 7安裝PostgresSQL9.3圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • pgsql查詢優(yōu)化之模糊查詢實例詳解

    pgsql查詢優(yōu)化之模糊查詢實例詳解

    這篇文章主要給大家介紹了關(guān)于pgsql查詢優(yōu)化之模糊查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用pgsql具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • postgresql~*符號的含義及用法說明

    postgresql~*符號的含義及用法說明

    這篇文章主要介紹了postgresql~*符號的含義及用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • PostgreSQL 自增語法的用法說明

    PostgreSQL 自增語法的用法說明

    這篇文章主要介紹了PostgreSQL 自增語法的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • postgres 連接數(shù)查看與設(shè)置操作

    postgres 連接數(shù)查看與設(shè)置操作

    這篇文章主要介紹了postgres 連接數(shù)查看與設(shè)置操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • PostgreSQL使用SQL實現(xiàn)俄羅斯方塊的示例

    PostgreSQL使用SQL實現(xiàn)俄羅斯方塊的示例

    基于PostgreSQL實現(xiàn)的俄羅斯方塊游戲項目Tetris-SQL,通過純SQL代碼和數(shù)據(jù)庫操作重構(gòu)了經(jīng)典游戲邏輯,展現(xiàn)了SQL語言的圖靈完備性和技術(shù)潛力,本文介紹PostgreSQL使用SQL實現(xiàn)俄羅斯方塊的示例,感興趣的朋友一起看看吧
    2022-04-04
  • PGSQL 實現(xiàn)把字符串轉(zhuǎn)換成double類型(to_number())

    PGSQL 實現(xiàn)把字符串轉(zhuǎn)換成double類型(to_number())

    這篇文章主要介紹了PGSQL 實現(xiàn)把字符串轉(zhuǎn)換成double類型(to_number()),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • PostgreSQL數(shù)據(jù)庫視圖及子查詢使用操作

    PostgreSQL數(shù)據(jù)庫視圖及子查詢使用操作

    這篇文章主要為大家介紹了PostgreSQL數(shù)據(jù)庫視圖及子查詢的使用操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2022-04-04
  • PostgreSQL常用字符串分割函數(shù)整理匯總

    PostgreSQL常用字符串分割函數(shù)整理匯總

    作為當前最強大的開源數(shù)據(jù)庫,Postgresql(以下簡稱pg)對字符的處理也是最為強大的,下面這篇文章主要給大家介紹了關(guān)于PostgreSQL常用字符串分割函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-07-07
  • docker安裝Postgresql數(shù)據(jù)庫及基本操作

    docker安裝Postgresql數(shù)據(jù)庫及基本操作

    PostgreSQL是一個強大的開源對象-關(guān)系型數(shù)據(jù)庫管理系統(tǒng),以其高可擴展性和標準化而著稱,這篇文章主要介紹了docker安裝Postgresql數(shù)據(jù)庫及基本操作的相關(guān)資料,需要的朋友可以參考下
    2025-03-03

最新評論

尼勒克县| 石台县| 铜梁县| 哈尔滨市| 长岛县| 资兴市| 宁波市| 固镇县| 巴林右旗| 万全县| 敖汉旗| 九龙县| 渝北区| 福建省| 会泽县| 天镇县| 长葛市| 香港 | 秦皇岛市| 洪湖市| 阳朔县| 大方县| 灵寿县| 建平县| 德清县| 犍为县| 剑河县| 弥勒县| 黄大仙区| 西盟| 手机| 敦化市| 博兴县| 东乡县| 铁岭市| 梁山县| 吉安县| 滦南县| 汶川县| 阳信县| 大连市|