【EJB】Developing EJB Applications -- Chapter6(EJB应用安全)

xiaoxiao2021-02-28  103

EJB应用安全

6.1 安全身份

       6.1.1关于EJB安全身份

       EJB可以指定在调用其他组件上的方法时使用的身份。这是EJB安全身份,也称为调用身份。

 

       默认情况下,EJB使用自己的主叫方身份。可以将身份设置为特定的安全角色。当您要构建分段安全模型时,使用特定的安全角色非常有用 - 例如,仅将一组组件的访问权限于内部EJB。

 

       6.1.2设置EJB的安全标识

       通过安全配置中的<security-identity>标记指定EJB的安全标识。如果不存在<security-identity>标签,则默认使用EJB的调用者身份。

 

       示例:将EJB的安全标识设置为与其调用者相同

       此示例将由EJB进行的方法调用的安全标识设置为与当前呼叫者的身份相同。如果不指定<security-identity>元素声明,则此行为是默认值。

<ejb-jar> <enterprise-beans> <session> <ejb-name>ASessionBean</ejb-name> <!-- ... --> <security-identity> <use-caller-identity/> </security-identity> </session> <!-- ... --> </enterprise-beans> </ejb-jar>

       示例:将EJB的安全标识设置为特定角色

       要将安全身份设置为特定角色,请使用<security-identity>标签内的<run-as>和<role-name>标签。

<ejb-jar> <enterprise-beans> <session> <ejb-name>RunAsBean</ejb-name> <!-- ... --> <security-identity> <run-as> <description>A private internal role</description> <role-name>InternalRole</role-name> </run-as> </security-identity> </session> </enterprise-beans> <!-- ... --> </ejb-jar>

       默认情况下,当您使用<run-as>时,名为匿名的主体将分配给传出呼叫。要分配不同的主体,请使用<run-as-principal>。

<session> <ejb-name>RunAsBean</ejb-name> <security-identity> <run-as-principal>internal</run-as-principal> </security-identity> </session>

       注意

       您还可以使用servlet元素内的<run-as>和<run-as-principal>元素。

 

6.2 EJB方法许可

       6.2.1 关于EJB方法权限

       EJB可以将访问方法限制在特定的安全角色。

 

       EJB<method-permission>元素声明指定可以调用EJB接口方法的角色。您可以为以下组合指定权限:

       *命名EJB的所有home和component接口方法

       *指定EJB的home或component接口的方法

       *一组具有重载名称的方法中的指定方法。

 

        6.2.2 使用EJB方法权限

        概述

        <method-permission>元素定义了允许访问由<method>元素定义的EJB方法的逻辑角色。几个示例演示了xml的语法。可能存在多方法权限语句,并且它们具有累积效应。        

        <method-permission>元素是<ejb-jar>描述符的<assembly-descriptor>元素的子元素。

 

        XML语法是使用注释进行EJB方法权限的替代方法。

 

        示例:允许角色访问EJB的所有方法

<method-permission> <description>The employee and temp-employee roles may access any method of the EmployeeService bean </description> <role-name>employee</role-name> <role-name>temp-employee</role-name> <method> <ejb-name>EmployeeService</ejb-name> <method-name>*</method-name> </method> </method-permission>

        示例:允许角色仅访问EJB的特定方法,并限制可以传递哪些方法参数

<method-permission> <description>The employee role may access the findByPrimaryKey, getEmployeeInfo, and the updateEmployeeInfo(String) method of the AcmePayroll bean </description> <role-name>employee</role-name> <method> <ejb-name>AcmePayroll</ejb-name> <method-name>findByPrimaryKey</method-name> </method> <method> <ejb-name>AcmePayroll</ejb-name> <method-name>getEmployeeInfo</method-name> </method> <method> <ejb-name>AcmePayroll</ejb-name> <method-name>updateEmployeeInfo</method-name> <method-params> <method-param>java.lang.String</method-param> </method-params> </method> </method-permission>

        示例:允许任何经过身份验证的用户访问EJB的方法

        使用<unchecked />元素允许任何经过身份验证的用户使用指定的方法。

<method-permission> <description>Any authenticated user may access any method of the EmployeeServiceHelp bean</description> <unchecked/> <method> <ejb-name>EmployeeServiceHelp</ejb-name> <method-name>*</method-name> </method> </method-permission>

        示例:完全排除使用特定的EJB方法

<exclude-list> <description>No fireTheCTO methods of the EmployeeFiring bean may be used in this deployment</description> <method> <ejb-name>EmployeeFiring</ejb-name> <method-name>fireTheCTO</method-name> </method> </exclude-list>

        示例:包含几个<method-permission>块的完整<assembly-descriptor>

<ejb-jar> <assembly-descriptor> <method-permission> <description>The employee and temp-employee roles may access any method of the EmployeeService bean </description> <role-name>employee</role-name> <role-name>temp-employee</role-name> <method> <ejb-name>EmployeeService</ejb-name> <method-name>*</method-name> </method> </method-permission> <method-permission> <description>The employee role may access the findByPrimaryKey, getEmployeeInfo, and the updateEmployeeInfo(String) method of the AcmePayroll bean </description> <role-name>employee</role-name> <method> <ejb-name>AcmePayroll</ejb-name> <method-name>findByPrimaryKey</method-name> </method> <method> <ejb-name>AcmePayroll</ejb-name> <method-name>getEmployeeInfo</method-name> </method> <method> <ejb-name>AcmePayroll</ejb-name> <method-name>updateEmployeeInfo</method-name> <method-params> <method-param>java.lang.String</method-param> </method-params> </method> </method-permission> <method-permission> <description>The admin role may access any method of the EmployeeServiceAdmin bean </description> <role-name>admin</role-name> <method> <ejb-name>EmployeeServiceAdmin</ejb-name> <method-name>*</method-name> </method> </method-permission> <method-permission> <description>Any authenticated user may access any method of the EmployeeServiceHelp bean</description> <unchecked/> <method> <ejb-name>EmployeeServiceHelp</ejb-name> <method-name>*</method-name> </method> </method-permission> <exclude-list> <description>No fireTheCTO methods of the EmployeeFiring bean may be used in this deployment</description> <method> <ejb-name>EmployeeFiring</ejb-name> <method-name>fireTheCTO</method-name> </method> </exclude-list> </assembly-descriptor> </ejb-jar>

6.3 EJB安全注释

      6.3.1 关于EJB安全注释      

      EJBjavax.annotation.security注释在JSR250中定义。

 

      EJB使用安全注释将关于安全性的信息传递给部署者。这些包括:

 

      @DeclareRoles

      声明哪些角色可用

      @RunAs

       配置组件的传播安全标识。

 

       6.3.2。使用EJB安全注释

       概述

       您可以使用XML描述符或注释来控制哪些安全角色能够在Enterprise JavaBeans(EJB)中调用方法。有关使用XML描述符的信息,请参阅使用EJB方法权限。

       部署描述符中明确指定的任何方法值都会覆盖注释值。如果部署描述符中未指定方法值,则使用使用注释设置的值。最重要的粒度是基于每个方法的。

 

        控制EJB安全权限的注释

        @DeclareRoles

        使用@DeclareRoles来定义哪些安全角色来检查权限。如果没有@DeclareRoles存在,则会从@RolesAllowed注释自动构建该列表。有关配置角色的信息,请参阅Java EE 7教程通过声明安全角色指定授权用户。

        @RolesAllowed,@PermitAll,@DenyAll

        使用@RolesAllowed列出允许哪些角色访问方法或方法。使用@PermitAll或@DenyAll来允许或拒绝所有角色使用方法或方法。有关配置注释方法权限的信息,请参阅Java EE 7教程通过声明安全角色指定授权用户。

       @RunAs

        使用@RunAs指定方法在从注释方法进行调用时使用的角色。有关使用注释配置传播的安全身份的信息,请参阅Java EE 7教程第49.2.3节“传播安全身份(运行方式)”。

 

       示例:安全注释示

@Stateless @RolesAllowed({"admin"}) @SecurityDomain("other") public class WelcomeEJB implements Welcome { @PermitAll public String WelcomeEveryone(String msg) { return "Welcome to " + msg; } @RunAs("tempemployee") public String GoodBye(String msg) { return "Goodbye, " + msg; } public String GoodbyeAdmin(String msg) { return "See you later, " + msg; } }

         在此代码中,所有角色都可以访问WelcomeEveryone方法。 GoodBye方法在进行呼叫时使用tempemployee角色。 只有管理角色可以访问方法GoodbyeAdmin,以及任何其他没有安全注释的方法。

 

6.4 远程访问EJBS

        6.4.1 与远程EJB客户端使用安全领域

        向远程调用EJB的客户端添加安全性的一种方法是使用安全领域。 安全领域是用户名/密码对和用户名/角色对的简单数据库。 该术语也用于Web容器的上下文中,具有稍微不同的含义。

 

        要验证安全领域中存在于EJB中的特定用户名/密码对,请按照下列步骤操作:

        *向域控制器或独立服务器添加新的安全域。

        *将以下参数添加到应用程序的类路径中的jboss-ejb-client.properties文件中。 此示例假定该连接由文件中的其他参数称为默认值。

remote.connection.default.username=appuser remote.connection.default.password=apppassword

       *在域或独立服务器上创建自定义Remoting连接器,该连接器使用新的安全领域。

       *将EJB部署到配置为使用配置文件与自定义Remoting连接器的服务器组,或者如果您不使用托管域,则将其部署到独立服务器。

 

       6.4.2。 添加新的安全领域

       1.运行管理CLI:

        启动jboss-cli.sh或jboss-cli.bat命令并连接到服务器。

        2.创建新的安全领域本身:

         运行以下命令在域控制器或独立服务器上创建名为MyDomainRealm的新安全领域。

         对于域实例,请使用以下命令:

/host=master/core-service=management/security-realm=MyDomainRealm:add()

         对于独立实例,请使用以下命令:

/core-service=management/security-realm=MyDomainRealm:add()

        3.创建名为myfile.properties的属性文件:

        对于独立实例,创建一个文件EAP_HOME /standalone / configuration / myfile.properties,对于域实例,创建一个文件EAP_HOME / domain / configuration / myfile.properties。这些文件需要具有文件所有者的读写权限。

chmod 600 myfile.properties

       4.创建对存储有关新角色信息的属性文件的引用:

       运行以下命令创建一个指向myfile.properties文件的指针,该文件将包含与新角色相关的属性。

       注意

      属性文件不会由附加的add-user.sh和add-user.bat脚本创建。它必须在外部创建。

 

       对于域实例,请使用以下命令:

/host=master/core-service=management/security-realm=MyDomainRealm/authentication=properties:add(path=myfile.properties)

       对于独立实例,请使用以下命令:

/core-service=management/security-realm=MyDomainRealm/authentication=properties:add(path=myfile.properties)

       您的新安全领域已创建。当您将用户和角色添加到新的领域时,信息将存储在与默认安全领域的单独文件中。您可以使用自己的应用程序或程序来管理此新文件。

       注意

       当使用add-user.sh脚本将用户添加到非默认文件(而不是application-users.properties)时,您必须传递参数--user-properties myfile.properties,否则将尝试使用应用程序-users.properties。

 

       6.4.3。将用户添加到安全领域

       1.运行add-user.sh或add-user.bat命令。打开终端并将目录更改为/ bin /目录。如果您在Red Hat Enterprise Linux或任何其他类UNIX操作系统上,请运行add-user.sh。如果您在Microsoft Windows Server上,请运行add-user.bat。

        2.选择是否添加管理用户或应用程序用户。对于此过程,键入b以添加应用程序用户。

        3.选择用户将被添加到的领域。默认情况下,唯一可用的领域是ApplicationRealm。如果您添加了自定义领域,则可以将用户添加到该领域。

        4.出现提示时,键入用户名,密码和角色。出现提示时,键入所需的用户名,密码和可选角色。通过键入yes验证您的选择,或键入否以取消更改。更改将写入安全领域的每个属性文件。

 

        6.4.4 安全域与安全领域之间的关系

        重要

        对于要由安全领域保护的EJB,他们必须使用一个安全域,该域被配置为从安全领域检索用户凭据。这意味着域需要包含Remoting和RealmDirect登录模块。分配安全域由@SecurityDomain注释完成,可以在EJB上应用。

 

       其他安全域从基础安全领域检索用户和密码数据。如果EJB上没有@SecurityDomain注释,则该安全域是默认域,但EJB中包含任何其他与安全相关的注释被认为是安全的。

       客户端用于建立连接的底层http-remoting连接器决定了使用哪个安全域。有关http-remoting连接器的更多信息,请参阅“JBoss EAP配置指南”中的“关于远程子系统”。

       默认连接器的安全领域可以这样改变:

/subsystem=remoting/http-connector=http-remoting-connector:write-attribute(name=security-realm,value=MyDomainRealm)

        6.4.5 关于使用SSL加密的远程EJB访问

        默认情况下,EJB2和EJB3 Bean的远程方法调用(RMI)的网络流量未加密。 在需要加密的情况下,可以使用安全套接字层(SSL),以便客户端和服务器之间的连接被加密。根据防火墙配置,使用SSL还具有允许网络流量穿越一些防火墙的附加优势。

 

       警告

       RedHat 建议在所有受影响的程序包中明确禁用SSLv2,SSLv3和TLSv1.0,以支持TLSv1.1或TLSv1.2。

      原文地址:

      https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.0/html/developing_ejb_applications/ejb_application_security

转载请注明原文地址: https://www.6miu.com/read-68457.html

最新回复(0)