oracle角色管理

摘自:http://blog.csdn.net/indexman/article/details/7897658

最近学习oracle角色管理,遂整理一下内容:

一 学习目标

     1.创建和修改角色

     2.控制角色的可用性

     3.移除角色

     4.使用预定义角色

     5.通过数据字典查询角色信息

二 角色的概念和特性

     1.什么是角色?

        角色就是相关权限的命令集合,使用角色的主要目的就是为了简化权限的管理。

     2.角色的特性有哪些?
       a.使用grant和revoke赋予和回收系统权限
       b.角色可以赋予给任何除自身之外的角色和用户
       c.角色可以由系统和对象权限组成
       d.可以启用和禁用角色
       e.可以指定一个密码
       f.角色不被任何用户拥有,不在任何方案内
       g.角色在数据字典中有各自的描述

三 创建、修改、分配角色

      1.语法如下:

      

   CREATEROLE role [NOTIDENTIFIED |IDENTIFIED

      {BY password | EXTERNALLY | GLOBALLY | USING package}]

  

  ALTERROLE role {NOT IDENTIFIED | IDENTIFIED

     {BY password |USING package| EXTERNALLY |GLOBALLY }};

 

  GRANT role [, role ]... 

    TO    {user|role|PUBLIC}

     [, {user|role|PUBLIC} ]...

  [WITH ADMIN OPTION] --被授予者可赋予其他用户权限


     2.例如:

       a. CREATE ROLE oe_clerk;   --普通

       b.CREATE ROLE hr_clerk IDENTIFIED BY bonus;   --指定密码

       c. CREATE ROLE hr_managerIDENTIFIED EXTERNALLY; --外部认证

     

       d. ALTER ROLE oe_clerk IDENTIFIEDBY order;

       e. ALTER ROLE hr_clerk IDENTIFIED EXTERNALLY;

       f.  ALTER ROLE hr_managerNOT IDENTIFIED;


      g. GRANT oe_clerk TOscott;

      h. GRANT hr_clerk TOhr_manager;

      i.  GRANT hr_manager TOscott WITH ADMIN OPTION;


    注:USING package适用于application role[应用角色],只有通过相关的包模块才能启用。


四 预定义角色

     

      预定义角色是指Oracle所提供的角色,每种角色都用于执行一些特定的管理任务,下面我们介绍常用的预定义角色connect,resource,dba。

     1、connect角色

           connect角色具有一般应用开发人员需要的大部分权限,当建立了一个用户后,多数情况下,只要给用户授予connect和resource角色就够了,

          那么connect角色具有以下系统权限:

          alter session

         create cluster

         create database link

         create session

         create view

         create sequence

     2、resource角色

        resource角色具有应用开发人员所需要的其他权限,比如建立存储过程、触发器等。这里需要注意的是resource角色隐含了unlimited tablespace系统权限。

        resource角色包含以下系统权限:

        create cluster

        create indextype

        create table

        create sequence

        create type

        create procedure

        create trigger

    3、dba角色

        dba角色具有所有的系统权限,及with admin option选项,默认的dba用户为sys和system他们可以将任何系统权限授予其他用户。

      但是要注意的是dba角色不具备sysdba和sysoper的特权(启动和关闭数据库)。


五  默认角色(default role)


      我们可以给某个用户分配一些角色,比如role r1,r2,r3 而其中可以将某些角色比如r1设置为default role,其他的不设置成default role,

    这样,当该用户登录时,自动具有default role中所包含的权限,其他的角色所具有的权限要通过set role 角色来获得。

    语法如下:

     ALTERUSER user DEFAULT ROLE  {role [,role]... | ALL [EXCEPT role[,role]... ] | NONE}

   

    下面我们举个例子:

     1. sys用户作为sysdba登录,创建3个角色:

      

  1. SQL> create role r1;  
  2.   
  3. 角色已创建。  
  4.   
  5. SQL> create role r2 identified by r2;  
  6.   
  7. 角色已创建。  
  8.   
  9. SQL> create role r3 identified by r3;  
  10.   
  11. 角色已创建。  
SQL> create role r1;

角色已创建。

SQL> create role r2 identified by r2;

角色已创建。

SQL> create role r3 identified by r3;

角色已创建。


    2. 给三个角色赋予权限:

  1. SQL> grant create session to r1;  
  2.   
  3. 授权成功。  
  4.   
  5. SQL> grant select on scott.emp to r2;  
  6.   
  7. 授权成功。  
  8.   
  9. SQL> grant insert on scott.dept to r3;  
  10.   
  11. 授权成功。  
SQL> grant create session to r1;

授权成功。

SQL> grant select on scott.emp to r2;

授权成功。

SQL> grant insert on scott.dept to r3;

授权成功。

     3.创建用户u1并将三个角色赋予:

  1. SQL> create user u1 identified by u1;  
  2.   
  3. 用户已创建。  
  4.   
  5. SQL> grant r1, r2, r3 to u1;  
  6.   
  7. 授权成功。  
SQL> create user u1 identified by u1;

用户已创建。

SQL> grant r1, r2, r3 to u1;

授权成功。

    4. 在修改用户u1的默认角色前,r1,r2,r3 角色均为u1的 default role,以u1用户登录,查询、新增,都没有问题。

  1. C:\Documents and Settings\Administrator>sqlplus u1/u1  
  2.   
  3. SQL*Plus: Release 10.2.0.1.0 - Production on 星期三 8月 22 23:21:25 2012  
  4.   
  5. Copyright (c) 1982, 2005, Oracle.  All rights reserved.  
  6.   
  7.   
  8. 连接到:  
  9. Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production  
  10. With the Partitioning, OLAP and Data Mining options  
  11.   
  12. SQL> select * from scott.emp where rownum<3;  
  13.   
  14.      EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM  
  15. ---------- ---------- --------- ---------- -------------- ---------- ----------   
  16.     DEPTNO  
  17. ----------   
  18.       7369 SMITH      CLERK           7902 17-12月-80            800  
  19.         20  
  20.   
  21.       7499 ALLEN      SALESMAN        7698 20-2月 -81           1600        300  
  22.         30  
  23. SQL> insert into scott.dept(deptno) values(80);  
  24.   
  25. 已创建 1 行。  
C:\Documents and Settings\Administrator>sqlplus u1/u1

SQL*Plus: Release 10.2.0.1.0 - Production on 星期三 8月 22 23:21:25 2012

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> select * from scott.emp where rownum<3;

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO
----------
      7369 SMITH      CLERK           7902 17-12月-80            800
        20

      7499 ALLEN      SALESMAN        7698 20-2月 -81           1600        300
        30
SQL> insert into scott.dept(deptno) values(80);

已创建 1 行。

       

       5. 现在sys用户修改用户u1的default role,仅将r1作为u1的默认角色:

            alter user u3 default role r1;  --此时将覆盖原来的设置,u1 的default role =r1,仅仅有登录权限。

 

  1. C:\Documents and Settings\Administrator>sqlplus u1/u1  
  2.   
  3. SQL*Plus: Release 10.2.0.1.0 - Production on 星期三 8月 22 23:29:05 2012  
  4.   
  5. Copyright (c) 1982, 2005, Oracle.  All rights reserved.  
  6.   
  7.   
  8. 连接到:  
  9. Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production  
  10. With the Partitioning, OLAP and Data Mining options  
  11.   
  12. SQL> select * from scott.emp where rownum<3;  
  13. select * from scott.emp where rownum<3  
  14.                     *  
  15. 第 1 行出现错误:  
  16. ORA-00942: 表或视图不存在  
  17.   
  18.   
  19. SQL> insert into scott.dept(deptno) values(80);  
  20. insert into scott.dept(deptno) values(80)  
  21.                   *  
  22. 第 1 行出现错误:  
  23. ORA-00942: 表或视图不存在  
C:\Documents and Settings\Administrator>sqlplus u1/u1

SQL*Plus: Release 10.2.0.1.0 - Production on 星期三 8月 22 23:29:05 2012

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> select * from scott.emp where rownum<3;
select * from scott.emp where rownum<3
                    *
第 1 行出现错误:
ORA-00942: 表或视图不存在


SQL> insert into scott.dept(deptno) values(80);
insert into scott.dept(deptno) values(80)
                  *
第 1 行出现错误:
ORA-00942: 表或视图不存在

       6. 用户自己打开role权限

           set role r2 identified by r2;

           set role r3 identified by r3;

        此时插入记录没有问题,但是select 确发现不行了。

 

  1. SQL> set role r2 identified by r2;  
  2.   
  3. 角色集  
  4. SQL> set role r3 identified by r3;  
  5.   
  6. 角色集  
  7.   
  8. SQL> insert into scott.dept(deptno) values(80);  
  9.   
  10. 已创建 1 行。  
  11.   
  12. SQL> select * from scott.emp where rownum<3;  
  13. select * from scott.emp where rownum<3  
  14.                     *  
  15. 第 1 行出现错误:  
  16. ORA-00942: 表或视图不存在  
SQL> set role r2 identified by r2;

角色集
SQL> set role r3 identified by r3;

角色集

SQL> insert into scott.dept(deptno) values(80);

已创建 1 行。

SQL> select * from scott.emp where rownum<3;
select * from scott.emp where rownum<3
                    *
第 1 行出现错误:
ORA-00942: 表或视图不存在

        证明此时用户所属的角色仅仅是默认角色r1,和刚刚打开的角色r3,而r2被set role r3 identified by r3; 覆盖掉了。

        那要同时有r2,r3 的权限怎么办呢?

           set role r2 identified by r2,r3 identified by r3; 此时就同时查询新增了。

  1. SQL> set role r2 identified by r2, r3 identified by r3;  
  2.   
  3. 角色集  
  4.   
  5. SQL> select * from scott.emp where rownum<3;  
  6.   
  7.      EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM     DEPTNO  
  8. ---------- ---------- --------- ---------- -------------- ---------- ---------- ----------   
  9.       7369 SMITH      CLERK           7902 17-12月-80            800                    20  
  10.       7499 ALLEN      SALESMAN        7698 20-2月 -81           1600        300         30  
  11. SQL> insert into scott.dept(deptno) values(80);  
  12.   
  13. 已创建 1 行。  
SQL> set role r2 identified by r2, r3 identified by r3;

角色集

SQL> select * from scott.emp where rownum<3;

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM     DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-12月-80            800                    20
      7499 ALLEN      SALESMAN        7698 20-2月 -81           1600        300         30
SQL> insert into scott.dept(deptno) values(80);

已创建 1 行。

        注:不过set role 的效果是临时的,只是当前session有效,其他的session无效,当结束当前session后再登录,又只有default role 的权限了。


 六 应用角色(application role)

        

Description of Figure 20-2 follows


通过上图可以一目了然知道应用角色的位置,oracle提供安全应用角色仅允许被授权的PL/SQL包启用。下面举例说明:

       1. 以sys用户登录,创建测试用户user1,测试角色role1,验证角色的存储过程p_enable_role;

           将role1授予user1,将执行存储过程p_enable_role权限赋予user1, 将select在hr.countries上的权限赋予role;

 

  1. SQL> create user user1 identified by user1;  
  2.   
  3. 用户已创建。  
  4.   
  5. SQL> create role role1 identified using sys.p_enable_role1;  
  6.   
  7. 角色已创建。  
  8.   
  9. SQL> create or replace procedure p_enable_role1 authid current_user is  
  10.   2  begin  
  11.   3  if sys_context('userenv','ip_address') ='192.168.0.166' then  
  12.   4  dbms_session.set_role('role1');  
  13.   5  else  
  14.   6  null;  
  15.   7  end if;  
  16.   8  end;  
  17.   9  /  
  18.   
  19. SQL> grant execute on p_enable_role1 to user1;  
  20.   
  21. 授权成功。  
  22.   
  23. SQL> grant select on hr.countries to role1;  
  24.   
  25. 授权成功。  
  26.   
  27. SQL> grant role1 to user1;  
  28.   
  29. 授权成功。  
SQL> create user user1 identified by user1;

用户已创建。

SQL> create role role1 identified using sys.p_enable_role1;

角色已创建。

SQL> create or replace procedure p_enable_role1 authid current_user is
  2  begin
  3  if sys_context('userenv','ip_address') ='192.168.0.166' then
  4  dbms_session.set_role('role1');
  5  else
  6  null;
  7  end if;
  8  end;
  9  /

SQL> grant execute on p_enable_role1 to user1;

授权成功。

SQL> grant select on hr.countries to role1;

授权成功。

SQL> grant role1 to user1;

授权成功。

        2. 将user1的默认role设置为none ,将createsession赋予user1这个用户


  1. SQL> alter user user1 default role none;  
  2.   
  3. 用户已更改。  
  4.   
  5. SQL> grant create session to user1;  
  6.   
  7. 授权成功。  
SQL> alter user user1 default role none;

用户已更改。

SQL> grant create session to user1;

授权成功。

       3. 在192.168.0.53这个ip上测试过程

  1. C:\Users\Administrator>sqlplus user1/user1@xu  
  2.   
  3. SQL*Plus: Release 10.2.0.1.0 - Production on 星期四 8月 23 12:46:22 2012  
  4.   
  5. Copyright (c) 1982, 2005, Oracle.  All rights reserved.  
  6.   
  7.   
  8. 连接到:  
  9. Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production  
  10. With the Partitioning, OLAP and Data Mining options  
  11.   
  12. SQL> select sys_context('userenv','ip_address'from dual;  
  13.   
  14. SYS_CONTEXT('USERENV','IP_ADDRESS')  
  15. --------------------------------------------------------------------------------   
  16. 192.168.0.53  
  17.   
  18. SQL> select sys_context('userenv','host')from dual;  
  19.   
  20. SYS_CONTEXT('USERENV','HOST')  
  21. --------------------------------------------------------------------------------   
  22. WORKGROUP\HUI  
  23.   
  24. SQL> select sys_context('userenv','server_host')from dual;  
  25.   
  26. SYS_CONTEXT('USERENV','SERVER_HOST')  
  27. --------------------------------------------------------------------------------   
  28. pc-20120517eoxo  
  29.   
  30. SQL> select * from session_roles;  
  31.   
  32. 未选定行  
  33.   
  34. SQL> exec sys.p_enable_role1;  
  35.   
  36. PL/SQL 过程已成功完成。  
  37.   
  38. SQL> select * from session_roles;  
  39.   
  40. 未选定行  
  41.   
  42. SQL> select * from hr.dept;  
  43. select * from hr.dept  
  44.                  *  
  45. 第 1 行出现错误:  
  46. ORA-00942: 表或视图不存在  
C:\Users\Administrator>sqlplus user1/user1@xu

SQL*Plus: Release 10.2.0.1.0 - Production on 星期四 8月 23 12:46:22 2012

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> select sys_context('userenv','ip_address') from dual;

SYS_CONTEXT('USERENV','IP_ADDRESS')
--------------------------------------------------------------------------------
192.168.0.53

SQL> select sys_context('userenv','host')from dual;

SYS_CONTEXT('USERENV','HOST')
--------------------------------------------------------------------------------
WORKGROUP\HUI

SQL> select sys_context('userenv','server_host')from dual;

SYS_CONTEXT('USERENV','SERVER_HOST')
--------------------------------------------------------------------------------
pc-20120517eoxo

SQL> select * from session_roles;

未选定行

SQL> exec sys.p_enable_role1;

PL/SQL 过程已成功完成。

SQL> select * from session_roles;

未选定行

SQL> select * from hr.dept;
select * from hr.dept
                 *
第 1 行出现错误:
ORA-00942: 表或视图不存在



       4. 在192.168.0.166上测试:

  1. C:\Documents and Settings\Administrator>sqlplus user1/user1@orcl  
  2.   
  3. SQL*Plus: Release 10.2.0.1.0 - Production on 星期四 8月 23 10:39:29 2012  
  4.   
  5. Copyright (c) 1982, 2005, Oracle.  All rights reserved.  
  6.   
  7.   
  8. 连接到:  
  9. Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production  
  10. With the Partitioning, OLAP and Data Mining options  
  11.   
  12. SQL> show user  
  13. USER 为 "USER1"  
  14. SQL> select sys_context('userenv','ip_address'from dual;  
  15.   
  16. SYS_CONTEXT('USERENV','IP_ADDRESS')  
  17. --------------------------------------------------------------------------------   
  18. 192.168.0.166  
  19.   
  20. SQL> select sys_context('userenv','host'from dual;  
  21.   
  22. SYS_CONTEXT('USERENV','HOST')  
  23. --------------------------------------------------------------------------------   
  24. WORKGROUP\PC-20120517EOXO  
  25.   
  26. SQL> select sys_context('userenv','server_host'from dual;  
  27.   
  28. SYS_CONTEXT('USERENV','SERVER_HOST')  
  29. --------------------------------------------------------------------------------   
  30. pc-20120517eoxo  
  31.   
  32. SQL> select role from session_roles;  
  33.   
  34. 未选定行  
  35.   
  36. SQL> exec sys.p_enable_role1;  
  37.   
  38. PL/SQL 过程已成功完成。  
  39.   
  40. SQL> select * from session_roles;  
  41.   
  42. ROLE  
  43. ------------------------------   
  44. ROLE1  
  45.   
  46. SQL> select * from hr.countries;  
  47.   
  48. CO COUNTRY_NAME                              REGION_ID  
  49. -- ---------------------------------------- ----------   
  50. AR Argentina                                         2  
  51. AU Australia                                         3  
  52. BE Belgium                                           1  
  53. BR Brazil                                            2  
  54. CA Canada                                            2  
  55. CH Switzerland                                       1  
  56. CN China                                             3  
  57. DE Germany                                           1  
  58. DK Denmark                                           1  
  59. EG Egypt                                             4  
  60. FR France                                            1  
  61. .....  
  62.   
  63. 已选择25行。  
C:\Documents and Settings\Administrator>sqlplus user1/user1@orcl

SQL*Plus: Release 10.2.0.1.0 - Production on 星期四 8月 23 10:39:29 2012

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> show user
USER 为 "USER1"
SQL> select sys_context('userenv','ip_address') from dual;

SYS_CONTEXT('USERENV','IP_ADDRESS')
--------------------------------------------------------------------------------
192.168.0.166

SQL> select sys_context('userenv','host') from dual;

SYS_CONTEXT('USERENV','HOST')
--------------------------------------------------------------------------------
WORKGROUP\PC-20120517EOXO

SQL> select sys_context('userenv','server_host') from dual;

SYS_CONTEXT('USERENV','SERVER_HOST')
--------------------------------------------------------------------------------
pc-20120517eoxo

SQL> select role from session_roles;

未选定行

SQL> exec sys.p_enable_role1;

PL/SQL 过程已成功完成。

SQL> select * from session_roles;

ROLE
------------------------------
ROLE1

SQL> select * from hr.countries;

CO COUNTRY_NAME                              REGION_ID
-- ---------------------------------------- ----------
AR Argentina                                         2
AU Australia                                         3
BE Belgium                                           1
BR Brazil                                            2
CA Canada                                            2
CH Switzerland                                       1
CN China                                             3
DE Germany                                           1
DK Denmark                                           1
EG Egypt                                             4
FR France                                            1
.....

已选择25行。

七  从数据字典获取角色信息

   

  例子:

 

  1. SQL> select role, password_required  
  2.   2  from dba_roles;  
  3.   
  4. ROLE                           PASSWORD  
  5. ------------------------------ --------   
  6. R1                             NO  
  7. R2                             YES  
  8. R3                             YES  
  9. ROLE1                          YES  
  10. ......  
  11. 已选择37行。  
  12.   
  13. SQL> select * from  dba_role_privs;  
  14.   
  15. GRANTEE                        GRANTED_ROLE                   ADM DEF  
  16. ------------------------------ ------------------------------ --- ---   
  17. SYS                            XDBADMIN                       YES YES  
  18. SYS                            IMP_FULL_DATABASE              YES YES  
  19. DBA                            OLAP_DBA                       NO  YES  
  20. DBA                            SCHEDULER_ADMIN                YES YES  
  21. SYSTEM                         AQ_ADMINISTRATOR_ROLE          YES YES  
  22. TSMSYS                         RESOURCE                       NO  YES  
  23. WMSYS                          RESOURCE                       NO  YES  
  24. WMSYS                          CONNECT                        NO  YES  
  25. SYSMAN                         MGMT_USER                      YES YES  
  26. SCOTT                          RESOURCE                       NO  YES  
  27. U1                             R2                             NO  NO  
  28.   
  29. SQL> select * from  role_sys_privs where role='R1';  
  30.   
  31. ROLE                           PRIVILEGE                                ADM  
  32. ------------------------------ ---------------------------------------- ---   
  33. R1                             CREATE SESSION                           NO  
  34.   
  35. SQL> set linesize 1000  
  36. SQL> select role, owner, table_name, privilege from role_tab_privs where role='ROLE1';  
  37.   
  38. ROLE                 OWNER                TABLE_NAME                     PRIVILEGE  
  39. -------------------- -------------------- ------------------------------ --------------------   
  40. ROLE1                HR                   COUNTRIES                      SELECT  
SQL> select role, password_required
  2  from dba_roles;

ROLE                           PASSWORD
------------------------------ --------
R1                             NO
R2                             YES
R3                             YES
ROLE1                          YES
......
已选择37行。

SQL> select * from  dba_role_privs;

GRANTEE                        GRANTED_ROLE                   ADM DEF
------------------------------ ------------------------------ --- ---
SYS                            XDBADMIN                       YES YES
SYS                            IMP_FULL_DATABASE              YES YES
DBA                            OLAP_DBA                       NO  YES
DBA                            SCHEDULER_ADMIN                YES YES
SYSTEM                         AQ_ADMINISTRATOR_ROLE          YES YES
TSMSYS                         RESOURCE                       NO  YES
WMSYS                          RESOURCE                       NO  YES
WMSYS                          CONNECT                        NO  YES
SYSMAN                         MGMT_USER                      YES YES
SCOTT                          RESOURCE                       NO  YES
U1                             R2                             NO  NO

SQL> select * from  role_sys_privs where role='R1';

ROLE                           PRIVILEGE                                ADM
------------------------------ ---------------------------------------- ---
R1                             CREATE SESSION                           NO

SQL> set linesize 1000
SQL> select role, owner, table_name, privilege from role_tab_privs where role='ROLE1';

ROLE                 OWNER                TABLE_NAME                     PRIVILEGE
-------------------- -------------------- ------------------------------ --------------------
ROLE1                HR                   COUNTRIES                      SELECT




 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值