2013-02-27 46 views
2

我一直努力遵循this答案主要是,但我總是重定向到我的login.xhtml(除了當我從登錄頁面登錄),因爲這...JSF登錄過濾器,會話是空

AppManager am = (AppManager) req.getSession().getAttribute("appManager"); 

始終爲空。 我一直在試圖打印出登錄屏幕上的用戶信息,無論我如何到達那裏所有字段(用戶名,密碼,登錄...)始終爲空,即使我直接從管理頁面輸入地址(這就是您登錄時獲得的位置)。 如何讓它保存會話,而不是每次我手動輸入地址/離開頁面時都會被截斷?

的AppManager:

import java.io.Serializable; 
import javax.ejb.EJB; 
import javax.enterprise.context.SessionScoped; 
import javax.faces.bean.ManagedBean; 
import javax.faces.context.FacesContext; 
import jobapp.controller.Controller; 

@ManagedBean(name="appManager") 
@SessionScoped 
public class AppManager implements Serializable { 
private static final long serialVersionUID = 16247164405L; 
    @EJB 
    private Controller controller; 
    private String username; 
    private String password; 
    private boolean loggedIn; 
    private Exception failure; 
    ... 
    /** 
    * 
    * @param e an exception to handle. 
    */ 
    private void handleException(Exception e) { 
     e.printStackTrace(System.err); 
     failure = e; 
     FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); 
    } 

    /** 
    * The login method. 
    * calls the controllers login method. 
    * 
    */ 
    public void login(){ 
     try{ 
      failure = null; 
      loggedIn = controller.login(username, password); 

     }catch (Exception e){ 
      handleException(e); 
     } 
    } 
    /** 
    * The logout method. 
    * Sets the user's info to null 
    * and stops the conversation. 
    */ 
    public void logout(){ 
     username = null; 
     password = null; 
     loggedIn = false; 
     FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); 
    } 
... 

篩選:

@WebFilter("/faces/admin.xhtml") 
public class LoginFilter implements Filter { 
... 
    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {  
     HttpServletRequest req = (HttpServletRequest) request; 
     //TODO fix "am" nullpointer 
     AppManager am = (AppManager) req.getSession().getAttribute("appManager"); 
     if (am != null && am.isLoggedIn()) { 
      // User is logged in, so just continue request. 
      chain.doFilter(request, response); 
     } else { 
      // User is not logged in, so redirect to login. 
      HttpServletResponse res = (HttpServletResponse) response; 
      res.sendRedirect(req.getContextPath() + "/faces/login.xhtml"); 
     } 
    } 
+0

爲了避免顯而易見,'@ SessionScoped'註釋來自'javax.faces.bean'包,對嗎? – BalusC 2013-02-27 18:14:22

+0

「我總是被重定向到我的login.xhtml(除了當我從登錄頁面登錄時)」這不就是重點嗎?用戶必須登錄或者'AppManager'爲空? – kolossus 2013-02-27 18:16:44

+0

@BalucC'@ SessionScoped'來自javax.enterprise.context.SessionScoped – 2013-02-27 18:54:33

回答

2

@SessionScoped是javax.enterprise.context.SessionScoped

結合這一個工程與CDI僅。在使用JSF @ManagedBean時,您應該使用javax.faces.bean包中的範圍註釋。

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 

@ManagedBean 
@SessionScoped 
public class AppManager implements Serializable { 

沒有一個有效的範圍內,一個JSF託管bean會表現得像@RequestScoped這實際上意味着它一次又一次地構建在每次請求。