日本韩国亚洲欧美在线-亚洲久悠悠色悠在线播放-最好看的中文字幕国语2019-四虎永久在线观看免费网站网址-青青草原精品国产亚洲AV-台湾佬电影网-久久精品国产AV一区二区三区-野战CHINESEVIDEO

技術(shù)知識
NEWS CENTRE
首頁
>
新聞中心
>
用戶登錄錯誤次數(shù)的實例代碼Postgresql
用戶登錄錯誤次數(shù)的實例代碼Postgresql
2021-08-24 閱讀:2455

文章主要介紹了Postgresql限制用戶登錄錯誤次數(shù)的實例代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下。

在oracle中我們可以通過設(shè)置FAILED_LOGIN_ATTEMPTS來限制用戶密碼登錄錯誤的次數(shù),但是在postgresql中是不支持這個功能的。盡管PostgreSQL支持event trigger,可是event局限于DDL,對于登錄登出事件是沒辦法使用event trigger的。

不過像登錄新建會話觸發(fā)某個事件這個需求可以通過hook實現(xiàn),不過該方法比較復(fù)雜,需要修改內(nèi)核代碼,在客戶端認證中添加邏輯,判斷輸入密碼次數(shù)統(tǒng)計。這里推薦一種比較簡單的方法實現(xiàn)類似的功能。

這里我們要使用到session_exec這個插件,使用該插件會在登錄時執(zhí)行一個指定的function。

下載地址:

https://github.com/okbob/session_exec

下載解壓之后需要進行以下配置:

set session_preload_libraries to session_execset

session_exec.login_name to name of your login function

該插件有以下特點:

如果函數(shù)不存在則會進行警告;

函數(shù)執(zhí)行失敗則不允許連接。

利用該插件我們可以寫一個簡單的函數(shù)來實現(xiàn)限制用戶登錄錯誤次數(shù)的功能。

例子:

1、建立外部表記錄數(shù)據(jù)庫日志信息。

CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;

CREATE FOREIGN TABLE pglog (
 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,
 backend_type text
) SERVER pglog
OPTIONS ( program 'find $PGDATA/log -type f -name "*.csv" -mtime -1 -exec cat {} \;', format 'csv' );

2、創(chuàng)建表t_login提取數(shù)據(jù)庫日志中的登錄信息。

create table t_login
(
login_time timestamp(3) with time zone --插入時間,
user_name text,
flag int --標(biāo)志位,0代表過期數(shù)據(jù)
);

 

插入登錄信息:

bill=# insert into t_login select log_time,user_name from pglog where command_tag='authentication' and error_severity= 'FATAL'
bill-# ;
INSERT 0 4

3、創(chuàng)建登錄執(zhí)行的function

create or replace function lock_user() returns void as $$
declare
res text;
c1 timestamp(3) with time zone;
begin
select login_time from t_login where flag = 0 order by login_time desc limit 1 into c1; --獲取當(dāng)前日志中最新時間
insert into t_login select log_time,user_name from pglog where command_tag='authentication' and error_severity= 'FATAL' and log_time > c1; --將最新的數(shù)據(jù)插入t_login表
update t_login set flag = 1 where login_time > c1;
for res in select user_name from t_login where flag = 1 group by user_name having count(*) >=3 --檢查登錄失敗次數(shù)是否大于3,若大于3則鎖定用戶
loop
EXECUTE format('alter user %I nologin',res); --鎖定用戶
EXECUTE 'select pg_terminate_backend(pid) from pg_stat_activity where usename=$1' using res; --斷開當(dāng)前被鎖定用戶會話
raise notice 'Account % is locked!',res;
end loop;
end;
$$ language plpgsql strict;

4、編輯postgresql.conf文件,配置登錄函數(shù)

session_preload_libraries='session_exec'
session_exec.login_name='lock_user'

 

5、測試

模擬test1用戶登錄錯誤超過3次:

bill=# select * from t_login;
     login_time     | user_name | flag
----------------------------+-----------+------
 2020-08-26 07:26:45.42+08 | test1   |  1
 2020-08-26 07:26:50.179+08 | test1   |  1
 2020-08-26 07:26:52.487+08 | test1   |  1
 2020-08-26 07:26:54.537+08 | test1   |  1
(4 rows)

 

當(dāng)我們在使用test1用戶登錄時則無法連接

pg13@cnndr4pptliot-> psql bill test1
Password for user test1:
NOTICE: c1 = <NULL>
psql: error: could not connect to server: FATAL: terminating connection due to administrator command
CONTEXT: SQL statement "select pg_terminate_backend(pid) from pg_stat_activity where usename=$1"
PL/pgSQL function lock_user() line 13 at EXECUTE

再次登錄可以看到提示該用戶被鎖定:

pg13@cnndr4pptliot-> psql bill test1
Password for user test1:
psql: error: could not connect to server: FATAL: role "test1" is not permitted to log in

 

6、解鎖用戶

此時想要解鎖該用戶則需要執(zhí)行:

bill=# alter user test1 login;
ALTER ROLE

然后需要注意還要將t_login中過期的數(shù)據(jù)修改。

bill=# update t_login set flag = 0;
UPDATE 4


13560189272
地址:廣州市天河區(qū)黃埔大道西201號金澤大廈808室
COPYRIFHT ? 2010-2020 廣州市名聯(lián)網(wǎng)絡(luò)科技有限公司 ALL RIGHTS RESERVED 粵ICP備10203057號
  • 這里是二維碼
99re6这里只有精品地址| 又色又爽又高潮免费视频观看| 99热国产免费| 99热在线观看这里只有精品| 五月丁香六月婷精品视频| 五月婷婷免费在线| 69堂午夜视频最新地址| 午夜精品777| 精品草原久久视频| 5月丁香六月情| 色色色.com| 亚洲视频图片婷婷五月| 婷婷激情五月天小说| 五月综合无码| 亚洲AV无码一区二区三区牛牛| 99热这里只有精彩| 51avj视频大全| 漂亮的丰满人妻中文字幕| 国产香蕉视频在线播放| 丰满高潮大叫少妇| 精品国产乱码久久久久久免费| 99久热这里只有精品视频删减版| 国产精品美女久久久久AⅤ| 欧美永久AAA| 婷婷爱五月| 久久精品视在线看6| 风流少妇A片一区二区蜜桃| 国产偷人爽久久久久久老妇APP| 777影视理论片大全在线观看| 人人爱天天做|