我正在为我们的应用程序创建单元测试,但是我被卡住了。为了测试,我有一个简单的HelloWebServlet,我通过注释来保护它:
@WebServlet(urlPatterns = {"/hello"}) @ServletSecurity(@HttpConstraint(rolesAllowed = {"user"}))
我正在以正常的方式启动服务器(参见1)并创建用户(参见2),因为对list-file-users和list-file-groups的CommandRunner调用的输出是正确的,但当我尝试使用用户名和密码进行连接时,收到以下错误:
警告: WEB9102: com.sun.enterprise.security.auth.login.common.LoginException:登录失败: Web登录失败:无法找到登录配置
调用代码使用Jersey客户端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());
}(注意:我尝试设置javax.enterprise.system.core.security.level=FINE,但调用失败,并返回: PlainTextActionReporterFAILURENo configuration found for javax.enterprise.system.core.security。见鬼!)
我已经对glassfish-embedded all-3.1.2.jar(我们的生产版本)和glassfish-embedded all-3.2-b06.jar进行了测试,结果相同。你认为怎样才能解决这个问题?我在GFE上挣扎和成功的次数太多了,没有战斗就放弃了!
==== 1服务器启动(摘录) ====
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用户创建(摘录) ====
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
);
}发布于 2012-06-21 17:57:58
我得到了同样的错误,并设法解决了它,尽管我的情况略有不同。我是从" maven -embedded-glassfish- plugin“maven插件启动GF 3.1.2的,但是这个插件可能也适用于你。
尝试设置以下系统属性:
java.security.auth.login.config=target/test-classes/login.conf这应该指向login.conf文件的副本。您可以在任何Glassfish域文件夹的"config“文件夹中找到此文件。
https://stackoverflow.com/questions/10803517
复制相似问题