我们有一个Java EE 7应用程序并使用Arquillian来测试内容。现在我们要检查当前登录用户的一些权限。我的问题很基本,如何在测试用例中登录用户?我已阅读ProgrammaticLogin doesnt work in arquillian tests和Embedded Glassfish, security and Arquillian questions,但未明确回答。我目前的做法是这样的:
// Inject services etc.
@Test
public void testLogin(){
UserAccount user = new UserAccount();
user.setUsername("bob");
user.setPassword("bob");
userAccountService.save(user);
ProgrammaticLogin pl = new ProgrammaticLogin();
String realmName = "secureJDBCRealm";
try {
pl.login("bob", "bob".toCharArray(), realmName, true);
} catch (Exception e){
e.printStackTrace();
}
}
现在当我尝试运行它时,得到一个LoginException,声称我没有为“fileRealm”配置LoginModule。但是“fileRealm”不是我正在寻找的领域(我把它放在那里进行第一次测试,但后来我将其改为“secureJDBCRealm”,这是我们为GlassFish定制的安全领域)。我们使用arquillian-glassfish-embedded-3.1
进行测试。
答案 0 :(得分:3)
Arquillian不为定义领域提供任何支持。相反,您需要自己在容器中配置域。使用嵌入式Glassfish容器时这有点棘手,但它是可行的。
我假设secureJDBCRealm
是一个自定义领域,而不是标准/内置Glassfish领域之一。要在嵌入式Glassfish容器中配置自定义域,您需要:
在引用该领域的测试类路径上放置一个login.conf
文件。为此,请将config目录添加到资源目录,并将login.conf
放在该目录中。您的login.conf
看起来像这样
secureJDBCRealm {
com.blah.blah.LoginModule required;
};
您的自定义领域以及任何依赖项都需要位于测试类路径上。
您需要以编程方式在glassfish中创建领域。这可以通过org.glassfish.embeddable.CommandRunner完成。幸运的是,Arquillian Embedded Container通过JNDI提供了这个功能,这意味着您可以执行以下操作:
@Resource(mappedName = "org.glassfish.embeddable.CommandRunner") CommandRunner commandRunner;
public void configureLoginRealm() {
CommandResult commandResult = commandRunner.run("create-auth-realm", "--classname=com.blah.blah.SecureJDBCRealm", "--property=jaas-context= secureJDBCRealm", "secure-JDBC-realm");
log.debug(commandResult.getExitStatus().toString() + " " + commandResult.getOutput());
Throwable throwable = commandResult.getFailureCause();
if (throwable != null) {
log.error(throwable.getMessage(), throwable);
}
}
}
然后,您可以使用
以编程方式登录ProgrammaticLogin pl = new ProgrammaticLogin();
String realmName = "secureJDBCRealm";
try {
pl.login("bob", "bob".toCharArray(), realmName, true);
} catch (Exception e){
e.printStackTrace();
} finally {
pl.logout();
}