JSF login filter, session is null

2019-02-25 18:03发布

I've been trying to follow this answer primarily but I always get redirected to my login.xhtml (except for when i log in from the login page) because this...

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

Is always null. I've been trying to print out user info on the login screen and no matter how i get there all fields(username, password, loggedIn...) are always null, even if i type the adress straight from the admin page (that's where you get when you log in). How do I make it so that the session is saved, not whiped everytime i type in the adress manually/leave the page?

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();
    }
...

Filter:

@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");
        }
    }

1条回答
叛逆
2楼-- · 2019-02-25 18:58

@SessionScoped is from javax.enterprise.context.SessionScoped

This one works in combination with CDI @Named only. As you're using JSF @ManagedBean, you should be using the scope annotations from javax.faces.bean package instead.

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

@ManagedBean
@SessionScoped
public class AppManager implements Serializable {

Without a valid scope, a JSF managed bean would behave like @RequestScoped which effectively means that it's constructed again and again on every request.

查看更多
登录 后发表回答