2011-07-05 116 views
2

我已成功設置JBoss安全域,並且可以使用BASIC身份驗證(如web.xml中定義的)進行身份驗證。這一切運作良好。但是我不知道如何使用http request.login方法。在JBoss/JAAS中使用HTTP Request.login

下面的安全域(的jboss-web.xml)中工作的基本身份驗證:

<jboss-web> 
    <context-root>/myapp</context-root> 
    <security-domain>java:/jaas/myapp-realm</security-domain> 
</jboss-web> 

但是當我使用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); 
    } 
} 

我得到下面的異常:

javax.servlet.ServletException: Failed to authenticate a principal 

我知道用戶名/密碼是好的(使用BASIC auth可以正常工作)。我有TRACE級別的日誌記錄,它看起來並不像它甚至試圖進行身份驗證。我錯過了什麼?

請參閱http://java-web-development.blogspot.com/2011/07/jee-6-security-part-two-implementation.html如果您需要有關我的設置/配置的更多詳細信息。我正在使用JBoss 6.

回答

5

它現在正在工作。我確保基於FORM的身份驗證工作,一旦運行,我又回到使用request.login,它的工作原理?!我通過JRebel使用熱部署,因此我有可能使用BASIC身份驗證進行身份驗證,並在會話中留下了一個用戶主體,然​​後導致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); 
    }