我们有这样一个需求:在数据库中建立两个用户,用户 A 用于创建一些视图,直接访问自己数据库中一个模式下的表,以及通过数据库链路访问其他数据库中的表;另一个用户 B 能访问到这个用户 A 中的视图,并且只能访问视图,且访问连接数有限制。
( miki 西游的文档,原文链接 http://mikixiyou.iteye.com/blog/1543530 )
这个用户 B 是用来给其他系统访问的,因此对权限和资源使用都需要有限制条件。
这种需求在很多行业的应用中都很常见。假如这是一道面试题,您该如何去回答呢?
我采用下面的方法来回答这个问题。
第一步,创建新用户 A 和 B 。
这里用户名称分别为 ryd_interface_src 和 ryd_interface ,对这两个用户都授予非常有限的权限。
drop user ryd_interface_src cascade;
create user ryd_interface_src identified by ryd_interface_src;
grant connect,create view to ryd_interface_src;
drop user ryd_interface cascade;
create user ryd_interface identified by ryd_interface;
grant connect,create synonym to ryd_interface;
第二步,登录数据库一个模式中,授权给用户 A ,使得用户 A 能创建视图
conn xxyyclient/xxyyclient
grant select on INVEST_CLOCK to ryd_interface_src with grant option;
grant select on INVEST_LOG to ryd_interface_src with grant option;
这里授权方法加了一个 with grant option ,请注意。
第三步,登录数据库用户 A 中,创建视图
conn ryd_interface_src/ryd_interface_src
create or replace view run_views as
select id as doc_id, title,fbsj as upload_date,'' as branch_code from xxyy.runs_lantern@CLIENT_XXYYWEB
where EXT1='1' and sysdate > START_TIME
and sysdate < SOLID_TIME
union
select a.doc_id,a.title,a.upload_date,a.branch_code from xxyy.cms_doc_single_attr@CLIENT_XXYYWEB a
left join xxyy.cms_doc_category_map@CLIENT_XXYYWEB b on a.doc_id=b.doc_id
where
a.state =1 and a.is_delete =0
and a.upload_date> sysdate-90
and b.cat_id=4;
create or replace view INVEST_CLOCK_VIEWS as
select * from xxyyclient.INVEST_CLOCK;
create or replace view INVEST_LOG_VIEWS as
select * from xxyyclient.INVEST_LOG;
第四步,在数据库用户 A 中,将视图查询权限授予给用户 B
因为在步骤二中,加了 with grant option ,所以这里视图查询权限可以成功授予。
grant select on INVEST_CLOCK_views to ryd_interface;
grant select on INVEST_LOG_views to ryd_interface;
grant select on run_views to ryd_interface;
第五步,在数据库用户 B 中,检查视图能否查询得到,再创建同义词。
conn ryd_interface/ryd_interface
select count(*) from ryd_interface_src.run_views;
select count(*) from ryd_interface_src.INVEST_CLOCK_views;
select count(*) from ryd_interface_src.INVEST_LOG_views;
create synonym run_views for ryd_interface_src.run_views;
create synonym INVEST_CLOCK_views for ryd_interface_src.INVEST_CLOCK_views;
create synonym INVEST_log_views for ryd_interface_src.INVEST_log_views;
第三方的系统直接通过这个同义词就可以访问到用户 A 中的视图。这也是对系统安全的一种保护措施。第三方系统登录后,只能看到其同义词,其他的都不会访问得到。
第六步,限制资源使用
因为用户 B 是给第三方系统使用,我们无法控制第三方应用的质量,为了防止在应用出现异常连接时数据库会话数暴涨导致数据库整体服务出现故障,所以我们对该用户的数据库会
话数做一个限制。
这里采用 profile 的 sessions_per_user 功能,实现单个用户会话数的限制。
conn / as sysdba
alter system set resource_limite=true scope=both sid='*';
新建 profile, 初始限制为 1 ,用于测试。
create profile third_user limit SESSIONS_PER_USER 1 FAILED_LOGIN_ATTEMPTS unlimited;
alter user ryd_interface profile third_user;
将会话数调整到 30
alter profile third_user limit SESSIONS_PER_USER 30;
通过以上操作,我们就可以实现这个需求了。
再想一下,这个实现方法真的完全满足要求了吗?
请见下文: Oracle用户访问权限与 PUBLIC角色的关系 ( http://mikixiyou.iteye.com/blog/1546581 )
本文链接 : http://mikixiyou.iteye.com/blog/1543530 欢迎转载,请注明出处和作者,谢谢合作。