11.6.12. 在应用程序里使用安全域
要在应用程序里使用安全域,首先您必须在服务器配置文件里定义安全域并在应用程序的描述符文件里启用安全域。然后您必须添加所需的注解到使用安全域的 EJB。本节涵盖了在应用程序里使用安全域所需的步骤。
警告
过程 11.6. 配置你的应用程序以使用安全域
定义安全域
您需要在服务器的配置文件里定义安全域,然后在应用程序的描述符文件里启用它。在服务器的配置文件里配置安全域
安全域是在服务器配置文件的security子系统里配置的。如果 JBoss EAP 6 实例运行在受管域里,配置文件应该是domain/configuration/domain.xml。如果是独立服务器,则是standalone/configuration/standalone.xml文件。other、jboss-web-policy和jboss-ejb-policy都是 JBoss EAP 6 里默认提供的安全域。下面的 XML 示例是从服务器配置文件的security子系统里复制的。为执行更快的验证检查,安全域的cach-type属性指定了一个缓存。允许的值由用于简单表缓存的default或 Infinispan 缓存的infinispan。<subsystem xmlns="urn:jboss:domain:security:1.2"> <security-domains> <security-domain name="other" cache-type="default"> <authentication> <login-module code="Remoting" flag="optional"> <module-option name="password-stacking" value="useFirstPass"/> </login-module> <login-module code="RealmDirect" flag="required"> <module-option name="password-stacking" value="useFirstPass"/> </login-module> </authentication> </security-domain> <security-domain name="jboss-web-policy" cache-type="default"> <authorization> <policy-module code="Delegating" flag="required"/> </authorization> </security-domain> <security-domain name="jboss-ejb-policy" cache-type="default"> <authorization> <policy-module code="Delegating" flag="required"/> </authorization> </security-domain> </security-domains> </subsystem>你可以按需要用管理控制台或 CLI 配置其他的安全域。在应用程序的描述符文件里启用安全域
安全域是在应用程序的WEB-INF/jboss-web.xml文件里的<jboss-web>元素的<security-domain>子元素里指定的。下面的例子配置了一个名为my-domain的安全域。<jboss-web> <security-domain>my-domain</security-domain> </jboss-web>这只是你可以在WEB-INF/jboss-web.xml描述符里指定的许多设置中的一个。
在 EJB 里添加必需的注解
你可以用@SecurityDomain和@RolesAllowed注解在 EJB 里配置安全性。下面的 EJB 代码示例限制了具有guest角色的用户对other安全域的访问。package example.ejb3; import java.security.Principal; import javax.annotation.Resource; import javax.annotation.security.RolesAllowed; import javax.ejb.SessionContext; import javax.ejb.Stateless; import org.jboss.ejb3.annotation.SecurityDomain; /** * Simple secured EJB using EJB security annotations * Allow access to "other" security domain by users in a "guest" role. */ @Stateless @RolesAllowed({ "guest" }) @SecurityDomain("other") public class SecuredEJB { // Inject the Session Context @Resource private SessionContext ctx; /** * Secured EJB method using security annotations */ public String getSecurityInfo() { // Session context injected using the resource annotation Principal principal = ctx.getCallerPrincipal(); return principal.toString(); } }关于更多的代码示例,请参考 JBoss EAP 6 Quickstarts 集里的ejb-securityquickstart,你可以在红帽的客户门户找到这些例子。

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.