0

I've seen many posts like mine, but none could helped me.

this is my managed bean and it's sessionScoped, if the login is ok it redirects to index page else displays error

@ManagedBean
@SessionScoped
public class LoginBean implements Serializable  {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private static final String[] users = {"anna:qazwsx","kate:123456"};

private String username;
private String password;

private boolean loggedIn     
public String doLogin() {
    for (String user: users) {
        String dbUsername = user.split(":")[0];
        String dbPassword = user.split(":")[1];

        // Successful login
        if (dbUsername.equals(username) && dbPassword.equals(password)) {
            loggedIn = true;
            return "/tmpl/home/index.xhtml?faces-redirect=true";
        }
    }

    // Set login ERROR
    FacesMessage msg = new FacesMessage("Login error!", "ERROR MSG");
    msg.setSeverity(FacesMessage.SEVERITY_ERROR);
    FacesContext.getCurrentInstance().addMessage(null, msg);

    return "/login/login.xhtml";
 }

 public boolean isLoggedIn() {
    return loggedIn;
 }
}

The view, all is normal here , calls the doLogin method of managed bean

<h:form id="login-form">
 <h:messages />
 <h:outputText value="Nom d'utilisateur:"/>
 <h:inputText value="#{loginBean.username}" id="username"/>
 <br/>
 <h:outputText value="Mot de passe:"/>
 <h:inputSecret value="#{loginBean.password}" id="password"/>
 <br/>
 <h:commandButton id="button" value="Login" action="#{loginBean.doLogin}" />    
 <br/>
</h:form>

The filter: if the user is authenticated then the loginBean is not null and it's logged

public class LoginFilter implements Filter {

public void doFilter(ServletRequest request, ServletResponse response,   FilterChain chain) throws IOException, ServletException {      
    HttpSession session = ((HttpServletRequest) request).getSession(false);
    LoginBean loginBean = (session != null) ? (LoginBean) session.getAttribute("loginBean") : null;


    if (loginBean!=null)
        System.out.println(loginBean.getUsername());

    if (loginBean == null || !loginBean.isLoggedIn()) {
        System.out.println("here agai");
        String contextPath = ((HttpServletRequest)request).getContextPath();
        ((HttpServletResponse)response).sendRedirect(contextPath + "/login/login.xhtml");
    }         
    chain.doFilter(request, response);             
  }
}

Why my managed bean (loginBean) is null?

jsf
  • 68
  • 3
  • 13
  • Try it with `@javax.enterprise.context.SessionScoped`. – alexander Mar 13 '15 at 20:12
  • @Alexander, actually, the opposite. He's using the JSF ManagedBean annotation, so he needs to use `javax.faces.bean.SessionScoped`. – DavidS Mar 13 '15 at 20:32
  • Oh thanks guys, i was not importing javax.faces.bean.SessionScoped, i was using javax.enterprise.context.SessionScoped. – jsf Mar 13 '15 at 20:43
  • Oh, sorry - my bad! I am using `@SessionScoped` in a CDI-Bean. Sorry, for messing up! – alexander Mar 14 '15 at 12:39

1 Answers1

1

Did you confirm that you're using the correct SessionScoped annotation?

See here: JSF login filter, session is null

DavidS
  • 5,022
  • 2
  • 28
  • 55