Oracle限制用户和客户端登陆

2020-08-19 10:55:26 浏览数 (1)

日常我们可能需要限制一些客户端或者IP 访问某个用户

则可以通过trigger的方式实现

1.trigger内容

这边通过建立一个Trigger来限制登陆

代码语言:javascript复制
CREATE OR REPLACE TRIGGER global_logon_trg AFTER logon ON DATABASE
DECLARE
   p_session_user  varchar2(64);
   p_ip       varchar2(64);
BEGIN
   SELECT UPPER(SYS_CONTEXT('USERENV', 'SESSION_USER')) INTO p_session_user FROM DUAL;
   SELECT UPPER(SYS_CONTEXT('USERENV', 'IP_ADDRESS')) INTO p_ip FROM DUAL;
   DBMS_SESSION.SET_IDENTIFIER(p_session_user || '-' || p_ip);
   IF ((p_session_user = 'FWASSY') AND (p_ip IN ('10.65.5.189'))) THEN
      DBMS_SESSION.SET_IDENTIFIER('about to raise app_error..');
      RAISE_APPLICATION_ERROR(-20003,'You are not allowed to connect to the database');
   END IF;
END;
/

2. SYS_CONTEXT

如下为一些可用的参数,大家可以根据需求来进行指定

代码语言:javascript复制
select 
  SYS_CONTEXT('USERENV','TERMINAL') terminal, 
  SYS_CONTEXT('USERENV','LANGUAGE') language, 
  SYS_CONTEXT('USERENV','SESSIONID') sessionid, 
  SYS_CONTEXT('USERENV','INSTANCE') instance, 
  SYS_CONTEXT('USERENV','ENTRYID') entryid, 
  SYS_CONTEXT('USERENV','ISDBA') isdba, 
  SYS_CONTEXT('USERENV','NLS_TERRITORY') nls_territory, 
  SYS_CONTEXT('USERENV','NLS_CURRENCY') nls_currency, 
  SYS_CONTEXT('USERENV','NLS_CALENDAR') nls_calendar, 
  SYS_CONTEXT('USERENV','NLS_DATE_FORMAT') nls_date_format, 
  SYS_CONTEXT('USERENV','NLS_DATE_LANGUAGE') nls_date_language, 
  SYS_CONTEXT('USERENV','NLS_SORT') nls_sort, 
  SYS_CONTEXT('USERENV','CURRENT_USER') current_user, 
  SYS_CONTEXT('USERENV','CURRENT_USERID') current_userid, 
  SYS_CONTEXT('USERENV','SESSION_USER') session_user, 
  SYS_CONTEXT('USERENV','SESSION_USERID') session_userid, 
  SYS_CONTEXT('USERENV','PROXY_USER') proxy_user, 
  SYS_CONTEXT('USERENV','PROXY_USERID') proxy_userid, 
  SYS_CONTEXT('USERENV','DB_DOMAIN') db_domain, 
  SYS_CONTEXT('USERENV','DB_NAME') db_name, 
  SYS_CONTEXT('USERENV','HOST') host, 
  SYS_CONTEXT('USERENV','OS_USER') os_user, 
  SYS_CONTEXT('USERENV','EXTERNAL_NAME') external_name, 
  SYS_CONTEXT('USERENV','IP_ADDRESS') ip_address, 
  SYS_CONTEXT('USERENV','NETWORK_PROTOCOL') network_protocol, 
  SYS_CONTEXT('USERENV','BG_JOB_ID') bg_job_id, 
  SYS_CONTEXT('USERENV','FG_JOB_ID') fg_job_id,
  from dual ;

3.注意事项

  • 该trigger对sys用户不生效
  • 对具有dba权限的用户不生效

0 人点赞