0 Replies Latest reply on Oct 31, 2005 7:08 PM by davidsan1001

    Way to implement regular sign-on authentication...and a prob

    davidsan1001

      So I managed to implement regular sign-in for the login page. Below is my code.

      This works well except that if an invalid user name or password is entered, I get a nasty stack trace with a java.util.NoSuchElementException.

      I've tried many ways to catch exception and redirect it to an error page with no luck. How do you do this with the current JSF implementation?

      Here is the code I promised...

      login.jsp (this basically replaces the form id=login)

      <h:form id="login2">

      User Name:
      <h:inputText value="#{userBean.userName}" />

      User Password:
      <h:inputText value="#{userBean.userPassword}" />

      <h:commandButton action="#{userBean.login}" value="Log In" />
      </h:form>

      ...then you have the change the UserBean.java by adding support for password, getter setters, add support for session which is need to connect to database. Here is all of the code
      package org.jbpm.webapp.bean;

      import java.util.ArrayList;
      import java.util.Iterator;
      import java.util.List;

      import javax.faces.model.SelectItem;

      import org.jbpm.identity.User;
      import org.jbpm.identity.hibernate.IdentitySession;
      import org.jbpm.security.Authentication;
      import org.jbpm.webapp.context.Context;
      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      import java.util.NoSuchElementException;

      public class UserBean {

      String userName;
      String userPassword;
      String authorizedUserName;

      public String getUserName() {
      return userName;
      }

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

      public String getUserPassword() {
      return userPassword;
      }

      public void setUserPassword(String password) {
      this.userPassword = password;
      }

      public String getAuthorizedUserName() {
      return authorizedUserName;
      }

      public void setAuthorizedUserName(String name) {
      this.authorizedUserName = name;
      }

      public String login() throws NoSuchElementException {
      // user got changed. update the jbpm authentication.
      setAuthorizedUserName(null);
      Authentication.popAuthenticatedActorId();

      IdentitySession identitySession = Context.getPersistenceContext().getIdentitySession();

      try {
      Object userid = identitySession.verify(userName, userPassword);
      setAuthorizedUserName (userName);
      Authentication.pushAuthenticatedActorId(authorizedUserName);
      return "home";
      } catch (NoSuchElementException e){return "login_error";}
      }



      public List getUsers() {
      IdentitySession identitySession = Context.getPersistenceContext().getIdentitySession();
      return identitySession.getUsers();
      }

      public List getUserSelectItems() {
      List userSelectItems = new ArrayList();

      IdentitySession identitySession = Context.getPersistenceContext().getIdentitySession();
      Iterator iter = identitySession.getUsers().iterator();
      while (iter.hasNext()) {
      User user = (User) iter.next();
      userSelectItems.add(new UserSelectItem(user));
      }

      return userSelectItems;
      }

      public String getLabel() {
      return userName;
      }

      public String getValue() {
      return userName;
      }

      public static class UserSelectItem extends SelectItem {
      private static final long serialVersionUID = 1L;
      public UserSelectItem(User user) {
      setValue(user.getName());
      setLabel(user.getName());
      }
      }
      private static final Log log = LogFactory.getLog(UserBean.class);
      }