在JBoss/JAAS中使用HTTP Request.login

Sim*_*mon 2 java security jaas java-ee-6 jboss6.x

我已经成功设置了JBoss安全域,并且可以使用BASIC身份验证进行身份验证(如web.xml中所定义).这一切都运作良好.但我无法弄清楚如何使用http request.login方法.

以下安全域(来自jboss-web.xml)适用于BASIC身份验证:

<jboss-web>  
    <context-root>/myapp</context-root>  
    <security-domain>java:/jaas/myapp-realm</security-domain>  
</jboss-web> 
Run Code Online (Sandbox Code Playgroud)

但是当我使用request.login时如下:

public void login() {
    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
    try {
        request.login(username, password);
    }
    catch (ServletException ex) {
        java.util.logging.Logger.getLogger(UserLogin.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

javax.servlet.ServletException: Failed to authenticate a principal
Run Code Online (Sandbox Code Playgroud)

我知道用户名/密码很好(使用BASIC auth工作正常).我有TRACE级别登录,看起来甚至没有尝试进行身份验证.我错过了什么?

如果您需要有关我的setup/config的更多详细信息,请参阅http://java-web-development.blogspot.com/2011/07/jee-6-security-part-two-implementation.html.我正在使用JBoss 6.

Sim*_*mon 5

它现在正在运作.我确保基于FORM的身份验证工作,一旦工作,我回到使用request.login,它工作?!我通过JRebel使用热部署,因此我有可能使用BASIC auth进行身份验证,并在会话中留下了一个用户主体,然​​后导致request.login失败(如果您已经过身份验证,request.login会抛出异常).我发誓我已经完成了JBoss的重启,但这是我能想到的唯一合乎逻辑的事情.

我现在对登录进行了一次健全性检查,如下所示:

public void login() {
    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
    try {
        Principal userPrincipal = request.getUserPrincipal();
        if (request.getUserPrincipal() != null) {
            request.logout();
        }
        request.login(username, password);
        userPrincipal = request.getUserPrincipal();
        authUser = userDao.findByLogin(userPrincipal.getName());
    }
    catch (ServletException ex) {
        java.util.logging.Logger.getLogger(UserLogin.class.getName()).log(Level.SEVERE, null, ex);
    }
Run Code Online (Sandbox Code Playgroud)