为 OTN 撰稿
为 Oracle 技术网撰写技术文章,获得报酬的同时可提升技术技能。
了解更多信息
密切关注
OTN 架构师社区
OTN ArchBeat 博客 Facebook Twitter YouTube 随身播图标

使用 Oracle Platform Security Services (OPSS) 执行编程身份断言

Shailesh K. Mishra

使用 Oracle Platform Security Services API 执行编程身份断言

2013 年 12 月

简介

身份断言是在没有秘密凭证的情况下建立身份的过程(例如,没有相应的口令,仅根据用户名执行身份验证决策)。我们为什么需要这样一种机制?例如,某个应用程序代表登录用户发送了一个 Java 消息服务 (JMS) 消息。另一个应用程序处理此消息。现在的问题是:第二个应用程序应在哪种安全环境中处理此消息?有三种可能的选择:匿名用户、服务帐户用户(通常是一个固定的用户帐户,与发送消息的登录用户无关)或发送消息的用户。第一种选择是显而易见的。第二种选择可以用消息驱动的 bean 和“runAs”角色实现。本文提供第三种选择的解决方案。

本文假定读者熟知 Java Authentication and Authorization Service (JAAS) API 和 Oracle Platform Security Services (OPSS) API。

使用 OPSS API 执行身份断言

让我们继续上面的示例:要实现第三种选择,我们需要为向 JMS 队列发送消息的用户创建经过身份验证的主题。要创建经过身份验证的主题,我们通常需要户名和口令。虽然第一个应用程序的代码可以通过将用户名放在消息中来传播用户名(可从当前运行的 JAAS 主题检索到),但它没有口令。要创建经过身份验证的主题,我们将必须断言此用户身份。下面将介绍如何使用 OPPS API 达到此目的。

OPSS SubjectSecurity 和 ActionExecutor API

SubjectSecurity 类提供了 getActionExecutor 方法,其参数是用户名。其实现为:首先,根据身份存储断言指定的用户名,为已断言的用户创建 ActionExecutorActionExecutor 具有 execute 方法,其参数是 java.security.PrivilegedActionjava.security.PrivilegedExceptionActionPrivilegedAction/ PrivilegedExceptionAction 的 run 方法中的代码在已断言用户的安全环境中运行。请参阅以下 ActionExecutor Java 文档:

“只有 PrivilegedAction 或 PrivilegedExceptionAction 中运行的应用程序逻辑才会通过 ActionExecutor 自动与用户的平台安全环境关联。而对于 Weblogic 服务器,该逻辑通过调用 Security.runAs(Subject, PrivilegedAction) 与 WLS 主题关联。因此,应用程序操作所需的平台安全将由平台安全环境隐式处理,例如 EJB 调用和安全资源访问。”(原文如此)

SubjectSecurity 类的 getActionExecutor 方法受名为“IdentityAssertion”、操作为“execute”的代码权限 oracle.security.jps.JpsPermission 保护。调用代码源需要被授予上述权限才能调用此方法。

有关 SubjectSecurity API 和 ActionExecutor API 的详细信息,请参阅参考资料部分。

整合所有内容

继续我们的示例,要实现第三种选择,第二个应用程序必须为向 JMS 队列发送消息的用户创建 ActionExecutor,并在其 execute 方法中运行消息处理逻辑,如下所示:

/*
 * OPSS API SubjectSecurity.getInstance().getActionExecutor(this.username) is protected by 
 * code permission oracle.security.jps.JpsPermission with name "IdentityAssertion" and action
 * "execute". The application code sources need to be granted the above permission when 
 * invoking this method. Wrapping this call in AccessController.doPrivileged to avoid all 
 * upper stack codes to have this permission.
 */
try{    
     ActionExecutor  ae = AccessController.doPrivileged(new PrivilegedExceptionAction
	 <ActionExecutor>() {
                public ActionExecutor run() throws Exception{
try {
                        return  SubjectSecurity.getInstance().getActionExecutor(username);
                    } catch (JpsException e) {
                       //handle exception
                    } catch (AssertionException e) {
                     //handle exception
                    }
                }
            });
        //execute your business logic that needs to run under security context of given user.
        ae.execute(new PrivilegedExceptionAction<Object>() {
                public Object run() throws Exception{
                    //run your code here
                }
            });
        }catch(Exception e){
          e.printStackTrace();
        }

授予代码源权限

运行以下 wlst 命令,提供使用 SubjectSecurity API 的 getActionExecutor 方法所需的权限。有关与 OPSS 相关的 wlst 命令的详细信息,请参阅参考资料部分。

grantPermission(codeBaseURL=""url of your code source>", 
permClass="oracle.security.jps.JpsPermission", permTarget="IdentityAssertion",
permAction="execute")

总结

本文介绍了必须在运行时身份的安全环境下运行代码时,如何使用 OPSS API 执行编程身份断言。

参考资料

关于作者

Shailesh K. Mishra 是 Oracle Identity Manager 团队成员。他获得了印度理工学院的技术学士学位,闲暇之余,他喜欢研究中间件性能和安全。