在使用代理用户时获取 Oracle 当前用户名

huangapple go评论60阅读模式
英文:

Oracle Get Current Username When Using a Proxy User

问题

可以在OracleDB中通过以下方式获取使用代理用户帐户连接的用户的真实用户名:

  1. 查询:
SELECT sys_context('userenv', 'session_user') FROM dual;

期望结果:

i_am_the_true_user_name

实际结果:

i_am_the_true_user_name
英文:

Is it possible to get the true user name of a user in OracleDB when the user in question is connected via a proxy user account?

True User Name: i_am_the_true_user_name
Proxy User Name: i_am_the_proxy_user_name

What I have tried:

  1. Query:
    Select user from dual;

Desired Result:
i_am_the_true_user_name
Actual Result:
i_am_the_proxy_user_name

  1. Query:
    SELECT username FROM user_users;

Desired Result:
i_am_the_true_user_name
Actual Result:
i_am_the_proxy_user_name

答案1

得分: 3

尝试这样做:

select sys_context('userenv','proxy_user') from dual;

代理账户名(开发者的个人账户)可在系统会话上下文中获得,并可以通过数据库触发器自动在v$session视图中显示,以便DBA随时了解谁连接到共享账户。注意:在实施之前,请确保您没有在其他地方使用 DBMS_SESSION.SET_IDENTIFIER 功能。

CREATE OR REPLACE TRIGGER db_session_trig 
AFTER LOGON ON DATABASE
  v_proxy_user varchar2;
BEGIN
  v_proxy_user := sys_context('userenv','proxy_user');
  if v_proxy_user is not null then
    dbms_session.set_identifier(v_proxy_user);
  end if;
END;
select username, osuser, client_identifier 
from v$session where username='I_AM_THE_TRUE_USER_NAME';
用户名                    操作系统用户    客户端标识 
----------------------- --------- ----------------------
I_AM_THE_TRUE_USER_NAME oracle    I_AM_THE_PROXY_USER_NAME
英文:

Try this:

select sys_context('userenv','proxy_user') from dual;

The proxy account name (the developer’s personal account) is available in the system session context, and can be automatically made visible in the v$session view through a database trigger so that the DBA can tell who is connected to shared accounts at all times. Note: make sure you're not using the DBMS_SESSION.SET_IDENTIFIER functionality for anything else before implementing this.

CREATE OR REPLACE TRIGGER db_session_trig 
AFTER LOGON ON DATABASE
  v_proxy_user varchar2;
BEGIN
  v_proxy_user := sys_context('userenv','proxy_user');
  if v_proxy_user is not null then
    dbms_session.set_identifier(v_proxy_user);
  end if;
END;

select username, osuser, client_identifier 
from v$session where username='I_AM_THE_TRUE_USER_NAME';

USERNAME                OSUSER    CLIENT_IDENTIFIER 
----------------------- --------- ----------------------
I_AM_THE_TRUE_USER_NAME oracle    I_AM_THE_PROXY_USER_NAME

huangapple
  • 本文由 发表于 2023年7月12日 23:59:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定