可开发 JACC 提供程序以对 Java™™ Platform
Enterprise Edition (J2EE) 应用程序制定定制授权决策,方法是实现 Liberty 概要文件服务器中提供的 com.ibm.wsspi.security.authorization.jacc.ProviderService 接口。
开始之前
缺省情况下,应用程序模块装入会延迟,直到处理针对该应用程序的请求,但是,准备处理应用程序之前,需要先处理应用程序中整个模块的安全性约束。延迟模块装入需要被禁用。下面显示如何对其进行禁用:
- 对于 WebContainer:
在
server.xml 文件中,需要设置以下元素:
<webContainer deferServletLoad="false"/>
- 对于 EJBContainer:
在
server.xml 文件中,需要设置以下元素:
<ejbContainer startEJBsAtAppStart="true"/>
注: 如果未设置以上元素,那么启动服务器时,完整安全性约束信息可能不会填充至第三方 JACC 提供程序。因此,第三方 JACC 提供程序可能无法强制实施正确的授权决策。
关于此任务
Java 容器授权合同规范 JSR 115
为授权提供程序定义接口。在 Liberty 概要文件服务器中,必须将 JACC 提供程序打包为用户功能部件。您的功能部件必须实现 com.ibm.wsspi.security.authorization.jacc.ProviderService 接口。
过程
- 创建 OSGi 组件,该组件提供用于实现 com.ibm.wsspi.security.authorization.jacc.ProviderService 接口的服务。
ProviderService
接口定义以下两个方法:getPolicy(Liberty 概要文件运行时调用此方法以检索用于实现 java.security.Policy
抽象类的 Policy 的实例)和 getPolicyConfigFactory(Liberty 概要文件运行时调用此方法检索用于实现
javax.security.jacc.PolicyConfigurationFactory 抽象类的 PolicyConfigurationFactory 类的实例)。
以下示例使用 OSGi 声明式服务注释:
@package com.mycompany.jacc;
import com.mycompany.jacc.MyAuthConfigProvider;
import com.ibm.wsspi.security.authorization.jacc.ProviderService;
// The property value of javax.security.jacc.policy.provider which defines the implementation class of Policy and
// javax.security.jacc.PolicyConfigurationFactory.provider which defines the implementation class of PolicyConfigurationFactory, are required for propagating the properties to the Liberty runtime.
@Component(service = ProviderService.class,
immediate = true,
property = {
"javax.security.jacc.policy.provider
=com.mycompany.jacc.MyPolicy",
"javax.security.jacc.PolicyConfigurationFactory.provider
=com.mycompany.jacc.MyPolicyConfigurationFactoryImpl"
}
)
public class MyJaccProviderService implements ProviderService {
// This method called by the Liberty profile runtime
// to get an instance of Policy class
@Override
public Policy getPolicy() {
return new myPolicy();
}
// This method called by the Liberty profile runtime
// to get an instance of PolicyConfigurationFactory class
@Override
public Policy getPolicyConfigurationFactory() {
ClassLoader cl = null;
PolicyConfigurationFactory pcf = null;
System.setProperty(JACC_FACTORY, JACC_FACTORY_IMPL);
try {
cl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(
this.getClass().getClassLoader());
pcf = PolicyConfigurationFactory.getPolicyConfigurationFactory();
} catch (Exception e) {
return null;
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
return pcf;
}
protected void activate(ComponentContext cc) {
// Read provider config properties here if needed,
// then pass them to the Provider ctor.
// This example reads the properties from the OSGi
// component definition.
configProps = (MapString, String) cc.getProperties();
}
protected void deactivate(ComponentContext cc) {}
}
- 将该组件与您的 JACC 提供程序一起打包成用户功能部件中的 OSGi 捆绑软件。
- 确保您的功能部件包含 OSGi 子系统内容:com.ibm.ws.javaee.jacc.1.5;
location:="dev/api/spec/"。
- 将功能部件安装到用户产品扩展位置之后,使用功能部件名称来配置 server.xml 文件。例如:
<featureManager>
...
<feature>usr:myJaccProvider</feature>
</featureManager>