SessionScope is not coming in Request

1

I have a bean to log in, when I try to get it in the filter class request it is coming null. The information that is coming from the console is these.

17:29:57,968 INFO  [stdout] (http-/127.0.0.1:8080-1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17:29:57,968 INFO  [stdout] (http-/127.0.0.1:8080-1) SETANDO O BOOLEAN DO    LOGIN E REDIRECIONANDO PARA O INDEX
17:29:57,968 INFO  [stdout] (http-/127.0.0.1:8080-1) USERNAME teste
17:29:57,969 INFO  [stdout] (http-/127.0.0.1:8080-1) PASSWORD teste
 17:29:57,969 INFO  [stdout] (http-/127.0.0.1:8080-1) BOOLEAN true
 17:29:57,969 INFO  [stdout] (http-/127.0.0.1:8080-1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17:29:57,969 INFO  [stdout] (http-/127.0.0.1:8080-1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17:29:57,975 INFO  [stdout] (http-/127.0.0.1:8080-1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17:29:57,975 INFO  [stdout] (http-/127.0.0.1:8080-1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17:29:57,975 INFO  [stdout] (http-/127.0.0.1:8080-1) org.jboss.weld.context.conversation.ConversationIdGenerator
17:29:57,976 INFO  [stdout] (http-/127.0.0.1:8080-1) org.jboss.weld.context.ConversationContext.conversations
17:29:57,976 INFO  [stdout] (http-/127.0.0.1:8080-1) org.jboss.weld.context.ignore.guard.marker
17:29:57,976 INFO  [stdout] (http-/127.0.0.1:8080-1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17:29:57,976 INFO  [stdout] (http-/127.0.0.1:8080-1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17:29:57,976 INFO  [stdout] (http-/127.0.0.1:8080-1) BEAN null OU BOOLEAN LOOGIN = null
17:29:57,976 INFO  [stdout] (http-/127.0.0.1:8080-1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17:29:57,976 INFO  [stdout] (http-/127.0.0.1:8080-1)     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Bean

@ManagedBean(name="login")
@SessionScoped
public class LoginBean implements Serializable {

private static final long serialVersionUID = 7765876811740798583L;

private boolean loggedIn;

private String userName;
private String passWord;

@Inject private LoginRepository loginRepository;



private UIComponent component; 

public String loginControl() {

    if (loginRepository.userLogin(userName, passWord) ) {
        this.loggedIn = true;

        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        System.out.println("SETANDO O BOOLEAN DO LOGIN E REDIRECIONANDO PARA O INDEX");
        System.out.println("USERNAME " + this.userName);
        System.out.println("PASSWORD " + this.passWord);
        System.out.println("BOOLEAN " + this.loggedIn);
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");

        return "/secured/index.xhtml?faces-redirect=true";
    }
    FacesContext context = FacesContext.getCurrentInstance();
    context.addMessage(component.getClientId(),new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro", "Usuário ou Senha Incorreto."));
    return "/login.xhtml";

}


public String efetuaLogoff() {
    FacesContext context = FacesContext.getCurrentInstance();
    context.getExternalContext().getSessionMap().remove("userName");
    FacesContext facesContext = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
    session.invalidate();
    this.loggedIn = false;
    return "/login.xhtml";
}


public String getUserName() {
    return userName;
}


public void setUserName(String userName) {
    this.userName = userName;
}


public String getPassWord() {
    return passWord;
}


public void setPassWord(String passWord) {
    this.passWord = passWord;
}

public UIComponent getComponent() {
    return component;
}

public void setComponent(UIComponent component) {
    this.component = component;
}

public boolean isLoggedIn() {
    return loggedIn;
}

public void setLoggedIn(boolean loggedIn) {
    this.loggedIn = loggedIn;
}

}

LoginFilter

public class LoginFilter implements Filter {

/**
 * Checks if user is logged in. If not it redirects to the login.xhtml page.
 */
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
    // Get the loginBean from session attribute
    LoginBean loginBean = (LoginBean)((HttpServletRequest) request).getSession().getAttribute("login");

    Enumeration<String> name = request.getAttributeNames();

    System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
    System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
    for(Enumeration<String> n = name; n.hasMoreElements();)
        System.out.println(n.nextElement());
    // For the first application request there is no loginBean in the
    // session so user needs to log in
    // For other requests loginBean is present but we need to check if user
    // has logged in successfully
    if (loginBean == null || !loginBean.isLoggedIn()) {
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        System.out.println("BEAN " + loginBean + " OU BOOLEAN LOOGIN = " + loginBean);
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");

        String contextPath = ((HttpServletRequest) request)
                .getContextPath();
        ((HttpServletResponse) response).sendRedirect(contextPath
                + "/login.xhtml");
    }

    chain.doFilter(request, response);

}

public void init(FilterConfig config) throws ServletException {
    // Nothing to do here!
}

public void destroy() {
    // Nothing to do here!
}

}

    
asked by anonymous 10.11.2014 / 20:38

1 answer

1

I solved the problem as follows.

I added the loggedIn variable in the session when the user is logged in, and in the filter I get this variable by the request I get. I do not know if this is the best way to do it, but it was a solution I found.

Bean

public String loginControl() {

    if (loginRepository.userLogin(userName, passWord) ) {
        this.loggedIn = true;

        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
        session.setAttribute("loggedIn", this.loggedIn);

        return "/secured/index.xhtml?faces-redirect=true";
    }
    FacesContext context = FacesContext.getCurrentInstance();
    context.addMessage(component.getClientId(),new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro", "Usuário ou Senha Incorreto."));
    return "/login.xhtml";

}

Filter

public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
    // Get the loginBean from session attribute
    //LoginBean loginBean = (LoginBean)((HttpServletRequest) request).getSession().getAttribute("login");
    Boolean loggedIn = false;

    try {
        loggedIn = (Boolean)((HttpServletRequest) request).getSession().getAttribute("loggedIn");
    }catch(NullPointerException e){
        System.out.println("Problema ao tentar logar.************************** erro " + e.getMessage());
        System.out.println("loggedIn = " + loggedIn);
    }

    if (loggedIn == null || !loggedIn) {

        String contextPath = ((HttpServletRequest) request).getContextPath();
        ((HttpServletResponse) response).sendRedirect(contextPath + "/login.xhtml");
    }

    chain.doFilter(request, response);

}
    
11.11.2014 / 12:30