“蓝图”安全性和OSGi 应用程序

您可以配置 bean 安全性,以便 bean 的方法仅可由分配了指定角色的用户访问。

You configure security by defining one or more <access-constraint> elements, inside the <bean> element for the bean whose security you want to configure, in the Blueprint XML file of your OSGi application. If you do not define an <access-constraint> element, the bean is not secured; this means that all the methods of the bean can be accessed by any user.

要使 Bean 安全性配置生效,必须在 WebSphere® Application Server中启用应用程序安全性。

可以在 bean 级别和方法级别配置 bean 安全性。 如果同时在 bean 级别和方法级别配置了 bean 安全性,那么会优先使用方法级别配置。

The <access-constraint> element is defined in a Blueprint namespace extension, https://www.ibm.com/appserver/schemas/blueprint/security/v1.0.0. Therefore, you must specify a namespace prefix for this extension in the <blueprint> element. 在随后的示例中,使用了前缀“sec”;按如下方式指定此前缀:
<blueprint
  xmlns="https://www.osgi.org/xmlns/blueprint/v1.0.0"
  xmlns:sec="https://www.ibm.com/appserver/schemas/blueprint/security/v1.0.0">

配置 bean 级别安全性

To configure security at the bean level, include a role attribute in the <access-constraint> element definition. bean 的方法仅可由分配了指定角色的用户访问。

在以下示例中,secureBean1 bean 的方法仅可由分配了称为“ROLE1”的角色的用户访问:
<bean
  id="secureBean1"
  class="com.ibm.ws.eba.wab.componenttest.blueprint.secure.BlueprintSecureServiceImpl">
  <sec:access-constraint role="ROLE1" />
</bean>

配置方法级别安全性

要在方法级别配置安全性,除了包括 role 属性之外,还要包括 method 属性。 指定的方法仅可由分配了指定角色的用户访问。

在以下示例中,secureBean1 bean 的 getPrice 方法仅可由分配了 ROLE1 角色的用户访问。 任何用户都可访问该 bean 的所有其他方法。
<bean
  id="secureBean1"
  class="com.ibm.ws.eba.wab.componenttest.blueprint.secure.BlueprintSecureServiceImpl">
  <sec:access-constraint method="getPrice" role="ROLE1" />
</bean>
在以下示例中,getPrice 方法仅可由分配了 ROLE1 角色的用户访问,并且 printReport 方法仅可由分配了 ROLE2 角色的用户访问。 所有其他方法可由分配了 ROLE3 角色的用户访问。
<bean
  id="secureBean1"
  class="com.ibm.ws.eba.wab.componenttest.blueprint.secure.BlueprintSecureServiceImpl">
  <sec:access-constraint method="getPrice" role="ROLE1" />
  <sec:access-constraint method="printReport" role="ROLE2" />
  <sec:access-constraint role="ROLE3" />
</bean>

指定角色

要将角色分配给用户或组,请使用 WebSphere Application Server 管理控制台中的 安全角色到用户或组映射 面板。