Embedded Glassfish, security and Arquillian questions

我的未来我决定 提交于 2019-11-30 02:22:54
Ivan Nikolaev

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");
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!