您的位置:首页 > 数据库 > Oracle

oracle代理用户

2015-06-29 10:33 615 查看
一描述:
在实际环境中我们可能有这样一种需求,就是使用用户A建立用户B的表。但我们又不能授予A create any table的权限(因为这样A用户就可以在所有用户下建立表),ORACLE似乎也没有更细粒度的权限可以授予给A用户。在这种情况下,TOM大叔告诉我们可以使用代理用户,实验过程见下文。
 
二环境
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod

PL/SQL Release 10.2.0.1.0 - Production

CORE    10.2.0.1.0      Production

TNS for Linux: Version 10.2.0.1.0 - Production

NLSRTL Version 10.2.0.1.0 - Production

 
[oracle@localhost ~]$ uname -a
Linux localhost.localdomain 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux
 
三实验过程
SQL> create user a identified by a;

User created.
SQL> SQL> grant create session, create procedure,create table to a;
Grant succeeded.
SQL>  create user b identified by b

  2  ;
User created.
SQL>  grant create session, create table, create procedure to b;

Grant succeeded.
SQL>  alter user b quota unlimited on users;
User altered.
 SQL> alter user b grant connect through a; -- 将a设置为b的代理用户

User altered.
 SQL> conn a[b]/a --这时用用户a的密码就可以登录用户B
Connected.
SQL> show user;
USER is "B"
SQL>

SQL> create table test1 (x int);
Table created.

SQL> conn b/b

Connected.

SQL> select * from tab;
TNAME                          TABTYPE  CLUSTERID

------------------------------ ------- ----------

TEST1                          TABLE

可以看到B用户下已经建立了表test1
SQL> conn a/a

Connected.

SQL> select * from tab;
no rows selected

而用户a下没有建立表test1
  这样我们就基本实现了我们的实验目的,但还有一个致命的问题。就是在这种情况下代理用户a可以建立用户b所能建立的所有对象类型,也就是说如果用户b拥有建立表和存储过程的权限,那么a作为用户b的代理用户也可以建立用户b的表和存储过程。
  能不能让用户a只有建立用户b表的权限呢,当然有!
结合role我们就可以轻松完成这个需求。
SQL> drop user a;

 

User dropped.
SQL> drop user b cascade;
User dropped.
SQL>  create role b_role1;

Role created.
 SQL>  create role b_role2;
Role created.
SQL> grant create procedure, create session to b_role1;

Grant succeeded.
SQL>  grant create table to b_role2;

Grant succeeded.
SQL> create user a identified by a;

User created.
SQL>  grant create session, create procedure to a;

Grant succeeded.
SQL> create user b identified by b default tablespace users quota 

unlimited on users;
  2  

User created.
SQL> SQL> grant b_role1 to b;--要完成这个需求我们必须将权限以角色的方式授予给用户b,并且将create table权限单独授予一个独立的角色,然后使用alter user.... connect through.....with role role_name(这里对应的是b_role2).如果我们给b用户授权时不使用角色而是直接授权: grant create table, create procedure ,create
session to b.那么执行alter user.... connect through.....with role  role_name(这里对应的是b_role2)后,a用户仍然可以建立用户b的存储过程。

Grant succeeded.
SQL>  grant b_role2 to b;

Grant succeeded.
 SQL>  alter user b grant connect through a with role b_role2;

User altered.
SQL> conn a[b]/a

ERROR:

ORA-01045: user B lacks CREATE SESSION privilege; logon denied   

Warning: You are no longer connected to ORACLE.

SQL> conn / as sysdba

Connected.

SQL> grant create session to b_role2;
Grant succeeded.
SQL> conn a[b]/a

Connected.

SQL> create table test(x int);
Table created.
SQL> create procedure p as begin null; end;

  2  

  3  /

create procedure p as begin null; end;

*

ERROR at line 1:

ORA-01031: insufficient privileges

SQL> 

SQL> show user;

USER is "B"

可以看到代理用户a可以建立b的表但不能建立用户b的存储过程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  代理用户