使用 Authid Current_User 为调用者授权
来源:互联网 发布:信天游手机炒股软件 编辑:程序博客网 时间:2024/04/29 05:36
我在一些技术论坛里面,常常看到有朋友问这种问题: 为什么我的用户具有DBA权限,却无法在存储过程里面创建一张普通表呢?
下面就结合具体案例来谈谈这个问题:
SQL> conn eric/eric;
Connected.
Connected.
SQL> select * from dba_role_privs where grantee='ERIC';
GRANTEE GRANTED_ROLE ADM DEF
------------------------------ ------------------------------ --- ---
ERIC DBA NO YES
ERIC CONNECT NO YES
ERIC RESOURCE NO YES
ERIC RECOVERY_CATALOG_OWNER NO YES
GRANTEE GRANTED_ROLE ADM DEF
------------------------------ ------------------------------ --- ---
ERIC DBA NO YES
ERIC CONNECT NO YES
ERIC RESOURCE NO YES
ERIC RECOVERY_CATALOG_OWNER NO YES
可以看到,用户eric拥有 DBA 权限!
用此用户创建一个存储过程:
create or replace procedure p_CreateTable
as
begin
execute immediate 'create table test_tb(id number)';
end p_CreateTable;
/
as
begin
execute immediate 'create table test_tb(id number)';
end p_CreateTable;
/
Procedure created.
运行时会发现,系统提示权限不足(insufficient privileges)!
SQL> exec p_CreateTable;
BEGIN p_CreateTable; END;
*
ERROR at line 1:
ORA-01031: insufficient privileges
ORA-06512: at "ERIC.P_CREATETABLE", line 3
ORA-06512: at line 1
BEGIN p_CreateTable; END;
*
ERROR at line 1:
ORA-01031: insufficient privileges
ORA-06512: at "ERIC.P_CREATETABLE", line 3
ORA-06512: at line 1
由上可以看到,即使拥有DBA role,也不能创建表。
即 role在存储过程中不可见!
查阅资料发现:
Oracle8i以前的版本,所有已编译存储对象,包括packages, procedures, functions, triggers, views等,只能以定义者(Definer)身份解析运行;
而Oracle8i及其后的新版本,Oracle引入调用者(invoker)权限,使得对象可以以调用者身份和权限执行。
遇到这种情况,通常解决方法是进行显式的系统权限: grant create table to eric;
但是,此方法太笨,因为有可能执行一个存储过程,需要很多不同权限(oracle对权限划分粒度越来越细)。
最好的方法是,利用 oracle 提供的方法,在创建存储过程时,加入 Authid Current_User 条件进行权限分配。
create or replace procedure p_CreateTable Authid Current_User
as
begin
execute immediate 'create table test_tb(id number)';
end p_CreateTable;
/
as
begin
execute immediate 'create table test_tb(id number)';
end p_CreateTable;
/
Procedure created.
SQL> exec p_CreateTable;
PL/SQL procedure successfully completed.
PL/SQL procedure successfully completed.
SQL> desc test_tb
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
成功啦!!!
由此可以引申出一个问题:
如果用户B要执行A用户的某存储过程,传统的解决方案是:
A用户将此存储过程的执行权限赋予B用户:
grant execute on p_test to B;
grant execute on p_test to B;
在B用户下创建一个同义词:
create synonym p_test for a.p_test;
create synonym p_test for a.p_test;
然后B用户就可以直接执行p_test了.
但是,如果使用 Authid Current_User 选项,在创建时给调用者授权,就简单多了!
- 使用 Authid Current_User 为调用者授权
- 使用 Authid Current_User 为调用者授权
- Authid Current_User的使用
- Authid Current_User的使用
- Authid Current_User的使用
- Oracle:Authid Current_User的使用
- Oracle:Authid Current_User的使用
- Oracle:Authid Current_User的使用
- Oracle:Authid Current_User的使用
- Oracle:Authid Current_User的使用
- Oracle:Authid Current_User的使用
- Oracle:Authid Current_User的使用(2)
- Oracle:Authid Current_User的使用2
- Authid current_user的用法
- Oracle之AUTHID CURRENT_USER
- Authid current_user的用法
- oracle Authid Current_User
- Oracle之AUTHID CURRENT_USER
- 四则职场寓言改善生活环境
- 百度一下,你就知道
- C++/C 程序员要掌握的问题集锦之二
- 新年雪景新气象3
- PHP沉思录之二
- 使用 Authid Current_User 为调用者授权
- 微软银光 Silverlight相关下载
- 程序员必读:“五险一金”详解!
- 破解linux的root用户权限密码
- 宝宝拉肚子治疗全面总结
- VC++ Windows平台字符透明编程大总结
- 电影《时空骇客》的科学:隐形传输和虫洞
- CComBSTR 析构时报错问题
- Struts2输入校验实例