0

I'm creating unit tests for our application, and I'm stuck. For testing I have a simple HelloWebServlet that I'm protecting via annotations:

@WebServlet(urlPatterns = {"/hello"}) @ServletSecurity(@HttpConstraint(rolesAllowed = {"user"}))

I'm starting the server the way that's always worked OK (see [1]) and creating users (see [2]) seems to be OK because output from CommandRunner calls to list-file-users and list-file-groups are correct, but I'm getting this error when I try to connect using the username and password:

WARNING: WEB9102: Web Login Failed: com.sun.enterprise.security.auth.login.common.LoginException: Login failed: Unable to locate a login configuration

The calling code uses the Jersey client API:

@Test
public void testPingServletLoggedIn() {
    Client client = Client.create();
    client.addFilter(new HTTPBasicAuthFilter(GlassFishServerHelper.USERNAME, "xx"));
    WebResource webResource = client.resource(GlassFishServerHelper.getBaseUri() + "/hello");
    ClientResponse clientResponse = webResource
            .accept(MediaType.TEXT_PLAIN)
            .get(ClientResponse.class);     // @GET
    assertEquals(ClientResponse.Status.OK, clientResponse.getClientResponseStatus());
}

(Note: I tried to set javax.enterprise.system.core.security.level=FINE but that call failed with: PlainTextActionReporterFAILURENo configuration found for javax.enterprise.system.core.security . Drat!)

I've tried this against both glassfish-embedded-all-3.1.2.jar (our production version) and glassfish-embedded-all-3.2-b06.jar with the same results. What do you think would solve this? I've struggled and succeeded way too many times with GFE to give up without a fight!

==== [1] server startup (excerpt) ====

public static void startServer() {
        GlassFishProperties gfProps = new GlassFishProperties();
        gfProps.setPort("http-listener", PORT);
        GLASSFISH = GlassFishRuntime.bootstrap().newGlassFish(gfProps);
        GLASSFISH.start();

        enableDefaultPrincipleToRoleMapping();
        createUsersAndGroups();

        ScatteredArchive archive = new ScatteredArchive(WEB_APP_NAME, ScatteredArchive.Type.WAR);
        File classesDir = new File("out/production/simple-security-servlet-test");
        archive.addClassPath(classesDir);
        DEPLOYER = GLASSFISH.getDeployer();
        APP_NAME = DEPLOYER.deploy(archive.toURI());


private static void enableDefaultPrincipleToRoleMapping() throws GlassFishException {
    CommandRunner cr = GLASSFISH.getCommandRunner();
    CommandResult result = cr.run("set",
            "server-config.security-service.activate-default-principal-to-role-mapping=true");
}

==== [2] user creation (excerpt) ====

private static void createUsersAndGroups() throws GlassFishException {
    CommandRunner commandRunner = GLASSFISH.getCommandRunner();
    File passwordFile = new File("password-file.txt");
    CommandResult result = commandRunner.run("create-file-user",
            "--passwordfile", passwordFile.getAbsolutePath(),
            "--groups", "user",
            USERNAME
    );
}
4

1 回答 1

1

尽管我的情况略有不同,但我遇到了同样的错误并设法解决了它。我从“maven-embedded-glassfish-plugin”maven 插件开始 GF 3.1.2,但这也可能对你有用。

尝试设置以下系统属性:

java.security.auth.login.config=target/test-classes/login.conf

这应该指向 login.conf 文件的副本。您可以在任何 Glassfish 域文件夹的“config”文件夹中找到此文件。

于 2012-06-21T09:57:57.633 回答