oracle authid current_user详解

在编写PLSQL程序时,对于授权的考虑很重要。ORACLE PLSQL中提供两种授权选择:

--AUTHID DEFINER (定义者权限):指编译存储对象的所有者。也是默认权限模式。

--AUTHID CURRENT_USER(调用者权限):指拥有当前会话权限的模式,这可能和当前登录用户相同或不同(alter session set current_schema 可以改变调用者Schema)

来看下官方的解释:

By default, stored procedures and SQL methods execute with the privileges of their owner, not their current user. Suchdefiner's rights subprograms are bound to the schema in which they reside, allowing you to refer to objects in the same schema without qualifying their names. For example, if schemas HR and OEboth have a table called departments, a procedure owned by HR can refer to departments rather than HR.departments. If user OE calls HR's procedure, the procedure still accesses the departments table owned by HR.

A more maintainable way is to use the AUTHID clause, which makes stored procedures and SQL methods execute with the privileges and schema context of the calling user. You can create one instance of the procedure, and many users can call it to access their own data.

默认情况,程序以其拥有者身份(定义者)执行。定义者权限的程序与其所在模式绑定,调用对象不需要加上模式完整名称。例如,假如模式HR和OE都有deparments表,HR拥有的程序可直接调用departments而不用HR.departments.而如果OE调用HR的程序,程序仍然调用的是HR的departments.

如果希望不同模式(schema)调用相同的程序却可以操作各自拥有的对象,就可以在定义程序的时候加上AUTHID CURRENT_USER。

                                 

下面举例说明2中授权机制:

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

C:\Users\Administrator>sqlplus sys/oracle@orcl as sysdba
查看一下sys模式下user_tables表记录数:
sys@ORCL> select count(*) from user_tables;

  COUNT(*)
----------
       972
创建2个对比函数:

get_count is default auth mode. When another user calls this function it will use SYS's user_tables

sys@ORCL> CREATE OR REPLACE FUNCTION get_count RETURN NUMBER AUTHID DEFINER IS
  2      table_count NUMBER;
  3    BEGIN
  4      SELECT COUNT(*) INTO table_count FROM user_tables;
  5
  6      RETURN table_count;
  7    END;
  8    /

函数已创建。

get_count2 is CURRENT_USER auth mode. When another user calls this function it will use its user_tables

sys@ORCL>  CREATE OR REPLACE FUNCTION get_count2 RETURN NUMBER AUTHID CURRE
NT_USER IS
  2      table_count NUMBER;
  3    BEGIN
  4      SELECT COUNT(*) INTO table_count FROM user_tables;
  5
  6      RETURN table_count;
  7    END;
  8    /

函数已创建。

下面进行授权操作:

sys@ORCL> grant execute on get_count to hr;

授权成功。

sys@ORCL> grant execute on get_count2 to hr;

授权成功。

sys@ORCL> conn hr/hr;
已连接。
hr@ORCL> SELECT sys.get_count FROM dual;

 GET_COUNT
----------
       972

hr@ORCL> SELECT sys.get_count2 FROM dual;

GET_COUNT2
----------
         7

结果一目了然。

定义者权限模式确保我们能控制对集中式DML操作。
而调用者权限模式则确保我们能控制对分布式数据的DML操作。


详细信息请参考资料:点击打开链接http://download.csdn.net/detail/indexman/6642375




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

Dylan    presents.


  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Oracle中,AUTHID是一个用来定义程序是否应该以当前用户的身份还是以定义者的身份来运行的属性。具体来说,AUTHID有两个选项,分别为AUTHID CURRENT_USERAUTHID DEFINER。 AUTHID CURRENT_USER表示程序在运行时将以当前用户的身份运行。这意味着程序将具有与当前用户相同的权限和角色。当程序以AUTHID CURRENT_USER编译时,它将使用当前用户的权限和角色来访问数据库对象。 AUTHID DEFINER表示程序在运行时将以定义者的身份运行。这意味着程序将具有与定义者相同的权限和角色。当程序以AUTHID DEFINER编译时,它将使用定义者的权限和角色来访问数据库对象。 以上是关于OracleAUTHID的解释。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【转】oracle authid current_user详解](https://blog.csdn.net/shcqupc/article/details/51320792)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [PLSQL专项学习之Oracle存储过程中authid current_userauthid definer](https://blog.csdn.net/weixin_42163563/article/details/90292776)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值