下面是一个触发器的例子:
复制代码 代码如下:
create or replace trigger logon_ip_control
after logon on database
declare
ip string(30);
user string(30);
begin
select sys_context(‘userenv’,’session_user’) into user from dual;
select sys_context(‘userenv’,’ip_address’) into ip from dual;
if user=’epay_user’
then
if ip not in (‘192.168.219.20′,’192.168.219.22’)
then raise_application_error(-20001,’user ‘||user||’ is not allowed to connect from ‘||ip);
end if;
end if;
end;
/
该触发器对用户epay_user进行了ip限制(只允许’192.168.219.20′,’192.168.219.22’,如果需要设置ip段,用%或?代替即可,如’192.168.219.%‘)。
下面看几个例子测试一下:
1)从非允许ip地址登陆 (192.168.219.21),连接失败
复制代码 代码如下:
[oracle@lxdb2 ~]$ sqlplus epay_user@pri
sql*plus: release 11.2.0.3.0 production on wed jul 3 19:23:48 2013
copyright (c) 1982, 2011, oracle. all rights reserved.
enter password:
error:
ora-00604: error occurred at recursive sql level 1
ora-20001: user epay_user is not allowed to connect from 192.168.219.21
ora-06512: at line 10
2)从允许ip地址登陆(192.168.219.22),连接成功
复制代码 代码如下:
[oracle@lxdb1 ~]$ sqlplus epay_user
sql*plus: release 11.2.0.3.0 production on wed jul 3 11:24:25 2013
copyright (c) 1982, 2011, oracle. all rights reserved.
enter password:
connected to:
oracle database 11g enterprise edition release 11.2.0.3.0 – 64bit production
with the partitioning, olap, data mining and real application testing options
3)从本地登陆(192.168.219.23)不受ip限制影响,连接成功
复制代码 代码如下:
[oracle@lxdb1 ~]$ sqlplus epay_user
sql*plus: release 11.2.0.3.0 production on wed jul 3 11:24:25 2013
copyright (c) 1982, 2011, oracle. all rights reserved.
enter password:
connected to:
oracle database 11g enterprise edition release 11.2.0.3.0 – 64bit production
with the partitioning, olap, data mining and real application testing options