Fehler beim abrufen der Sitzung vom FacesContext innerhalb eines Servlet-Filter

Nach autheticating meine user, ich möchte einen Verweis in der Sitzung des angemeldeten Benutzers.

Hier, wie ich es in den setCurrentUser Methode :

FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
session.setAttribute("CURRENT_USER", currentUser);

Leider die session Referenz ist immer null !

Alternativ habe ich versucht mit der sessionMap

FacesContext facesContext = FacesContext.getCurrentInstance();
Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
sessionMap.put("CURRENT_USER", currentUser);

Er kläglich gescheitert mit dieser Ausnahme :

java.lang.UnsupportedOperationException
    at java.util.AbstractMap.put(AbstractMap.java:186)
    (...)

Was mache ich falsch ?

Den vollständigen code von meinem controller

UserController.java

public class UserController implements Filter {
    private FilterConfig fc;
    private static final String CURRENT_USER = "CURRENT_USER";

    public void init(FilterConfig filterConfig) throws ServletException {
        fc = filterConfig;
        log(">> Filter initialized");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //Authenticate user
        //...

        //Save refernce in Session
        setCurrentUser(currentUser);

        //(...)
    }

    public static void setCurrentUser(User u) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
        session.setAttribute(CURRENT_USER, u);//session is always NULL
    }

    public static User getCurrentUser() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);       
        return (User)session.getAttribute(CURRENT_USER);
    }

    //...   
}

JSF 2.0
JBoss 5.1.0.GA

InformationsquelleAutor Stephan | 2012-06-13

Schreibe einen Kommentar