title: Programmatic Form Based Authentication
body: <p>I am trying to perform the programmatic form based authentication and it seems that 
  
  <p>My web.xml:</p>
  
  <pre><code>          &lt;web-app&gt;
  
              &lt;context-param&gt;
                  &lt;param-name&gt;javax.faces.PROJECT_STAGE&lt;/param-name&gt;
                  &lt;param-value&gt;Development&lt;/param-value&gt;
              &lt;/context-param&gt;
  
              &lt;servlet&gt;
                  &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
                  &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
                  &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
              &lt;/servlet&gt;
  
              &lt;servlet-mapping&gt;
                  &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
                  &lt;url-pattern&gt;*.jsf&lt;/url-pattern&gt;
              &lt;/servlet-mapping&gt;
  
              &lt;servlet-mapping&gt;
                  &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
                  &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt;
             &lt;/servlet-mapping&gt;
  
              &lt;welcome-file-list&gt;
                  &lt;welcome-file&gt;confirmauthentication.xhtml&lt;/welcome-file&gt;
              &lt;/welcome-file-list&gt;
  
              &lt;session-config&gt;
                   &lt;session-timeout&gt;10&lt;/session-timeout&gt;
              &lt;/session-config&gt;
  
              &lt;security-constraint&gt;
                  &lt;display-name&gt;Authentication Ex Login&lt;/display-name&gt;
                  &lt;web-resource-collection&gt;
                      &lt;web-resource-name&gt;SecuredArea&lt;/web-resource-name&gt;
                      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
                      &lt;http-method&gt;GET&lt;/http-method&gt;
                      &lt;http-method&gt;POST&lt;/http-method&gt;
                  &lt;/web-resource-collection&gt;
                  &lt;auth-constraint&gt;
                      &lt;description/&gt;
                      &lt;role-name&gt;*&lt;/role-name&gt;
                  &lt;/auth-constraint&gt;  
              &lt;/security-constraint&gt;
  
              &lt;login-config&gt;
                  &lt;auth-method&gt;FORM&lt;/auth-method&gt;
                  &lt;realm-name&gt;mysqldomain&lt;/realm-name&gt;
                  &lt;form-login-config&gt;
                  &lt;form-login-page&gt;/authentication.xhtml&lt;/form-login-page&gt;
                  &lt;form-error-page&gt;/error.xhtml&lt;/form-error-page&gt;
                  &lt;/form-login-config&gt;
              &lt;/login-config&gt;
  
              &lt;security-role&gt;
                  &lt;description/&gt;
                  &lt;role-name&gt;*&lt;/role-name&gt;
              &lt;/security-role&gt;
                  &lt;/web-app&gt;
  </code></pre>
  
  <p>My jsf page called authentication.xhtml:</p>
  
  <pre><code>            &lt;h:form&gt;
                  &lt;h:panelGrid border="0" columns="2"&gt; 
                     &lt;h:outputText value="Username"/&gt;
                     &lt;h:inputText value="#{beanJSF.userName}" required="true" /&gt;
  
                     &lt;h:outputText value="Password"/&gt;
                    &lt;h:inputSecret value="#{beanJSF.password}" required="true" /&gt;
  
                     &lt;h:commandButton value="Log in" action="#{beanJSF.submit}"&gt;
                           &lt;f:ajax execute="@form" render="@form" /&gt;
                     &lt;/h:commandButton&gt;
                     &lt;h:messages /&gt;    
                  &lt;/h:panelGrid&gt;
                &lt;/h:form&gt;
  </code></pre>
  
  <p>It seems that when I press the “Log in” button, the submit method is not called and I can’t figure out the reason.  The server.log doesn’t display anything when I press the button (This message is not displayed “("I am in the login method!!!!!!!!...”).</p>
  
  <p>My ManagedBean:</p>
  
  <pre><code>               @URLMappings(mappings={
                  @URLMapping(id="success", pattern = "/authentication/", viewId =    "/confirmauthentication.jsf")})
                  public class BeanJSF implements Serializable  {
  
              private String password;
              private String userName;
                  // User is the Entity
                  private User loggedUser;
  
  
  
              @EJB
              UserEJB services;
  
  
                  public String submit() throws IOException {
              System.out.println("I am in the login method!!!!!!!! " + getUserName()+ "  " + getPassword());   
  
                  FacesContext context = FacesContext.getCurrentInstance();
                  ExternalContext externalContext = context.getExternalContext();
                  HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
  
                  try {
                           request.login(userName, password);
  
  
                 User user = services.authenticationUser(userName, password);  
                      this.setLoggedUser(user);
                      return "home?faces-redirect-true";
  
                  } catch (ServletException e) {
                      // Handle unknown username/password in request.login().
                      context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Unknown login", null));
                      return null;
                  }
              }
  
              public void logout() throws IOException {
                  ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
                  externalContext.invalidateSession();
                  externalContext.redirect(externalContext.getRequestContextPath() + "/authentication.xhtml");
              }
  
                 //+ setters and getters for username, password and loggedUser
          }
  </code></pre>
  
  <p>And my EJB is:</p>
  
  <pre><code>                @Stateless
                  public class UserEJB{
  
  
                  @PersistenceContext(unitName = "PyPersistenceUnit")
                  private EntityManager entityManager;
  
                   public UserEJB(){}
  
                  @TransactionAttribute(TransactionAttributeType.REQUIRED)
                  public User authenticationUser(String userName, String password){
  
  
                  try{
                   User user = entityManager.createNamedQuery(User.FIND_USER,User.class). setParameter("userName", userName).setParameter("password", password).getSingleResult();
                   return user;
              }
              catch(NonUniqueResultException ex){
                  ex.printStackTrace();
                  return null;
              }
              catch(NoResultException ex){
                  ex.printStackTrace();
                  return null;
              }
                  }
  </code></pre>