我正在爲我們的應用程序創建單元測試,並且卡住了。爲了測試我有一個簡單HelloWebServlet我正在通過註解保護:簡單的GlassFish嵌入式安全失敗:登錄失敗:無法找到登錄配置
@WebServlet(URL模式= { 「/你好」}) @ServletSecurity(@HttpConstraint(rolesAllowed = { 「用戶」}))
我以啓動服務器的方式始終工作正常(請參閱[1])並創建用戶(請參閱[2])似乎是正常的,因爲CommandRunner對列表文件用戶和列表文件組的調用是正確,但當我嘗試使用用戶名和密碼進行連接時出現此錯誤:
警告:WEB9102:Web登錄失敗:com.sun.enterprise.security.auth.login.common.LoginException:登錄失敗:無法找到登錄公司nfiguration
調用代碼使用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配置中找到爲javax.enterprise.system.core.security。 Dlad!)
我已經試過這兩個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
);
}