Embedded Glassfish, security and Arquillian questions

后端 未结 1 1119
盖世英雄少女心
盖世英雄少女心 2020-12-28 10:08

I want to test my EJBs on an embedded Glassfish using Arquillian.

The important thing is that I have to have security up because my bean logic does some programmati

相关标签:
1条回答
  • 2020-12-28 10:37

    Glassfish standalone instance configuration may be used with Arquillian GlassFish Embedded containter adapter. You need to check security in your tests, so you must prepare your GlassFish instance configuration first. This can be achieved also by programmatic configuration (see https://stackoverflow.com/a/20411981/2169124), but for me configuration in test resources looks more natural. To configure users in file realm (used by default) you need to:

    1. Install standalone Glassfish server;
    2. Start Glassfish instance with asadmin command:

      $GLASSFISH_HOME/bin/asadmin start-domain
      
    3. Add users to file realm with asadmin command (stored in file named keyfile):

      $GLASSFISH_HOME/bin/asadmin create-file-user --user username
      
    4. Copy folders

      $GLASSFISH_HOME/glassfish/domains/domain1/config
      $GLASSFISH_HOME/glassfish/domains/domain1/docroot
      

      to test resources (for example src/test/resources/domain, assuming that you are using maven)

    5. In arquillian.xml define path to glassfish configuration (it will be copied to target/test-classes/domain by maven):

      <?xml version="1.0" encoding="UTF-8"?>
      <arquillian xmlns="http://jboss.org/schema/arquillian"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="
          http://jboss.org/schema/arquillian
          http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
      <container qualifier="glassfish-embedded" default="true">
          <configuration>
              <property name="instanceRoot">target/test-classes/domain</property>
          </configuration>
      </container>
      

    6. Map users to roles in glassfish-application.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE glassfish-application PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Java EE Application 6.0//EN" "http://glassfish.org/dtds/glassfish-application_6_0-1.dtd">
      <glassfish-application>
          <security-role-mapping>
              <role-name>admin</role-name>
              <principal-name>username</principal-name>
          </security-role-mapping>
      </glassfish-application>
      
    7. Add glassfish-application.xml to test artifact:

      ShrinkWrap.create(EnterpriseArchive.class)
              .addAsModule(ejbJar)
              .addAsManifestResource(new File("src/test/resources/glassfish-application.xml"))
      
    8. In test method use ProgrammaticLogin to handle authentication:

      ProgrammaticLogin programmaticLogin = new ProgrammaticLogin();
      programmaticLogin.login("username", "password");
      
    0 讨论(0)
提交回复
热议问题