5 Replies Latest reply on Feb 1, 2007 3:17 PM by Mariusz Smykula

    Access to EntityManager in authenticator?

    Dustin Norlander Apprentice

      I am trying to add the new security features to my app, and am getting stuck trying to authenticate.

      Here is my class (almost exactly the same as the docs)

      @Name("authenticator")
      public class Authenticator {
      
       @In
       private EntityManager entityManager;
      
       @Logger Log log;
       public boolean authenticate(String username, String password, Set<String> roles)
       {
       log.info("Authenticating user " + username);
       log.info("em " + entityManager);
       try
       {
       User user = (User) entityManager.createQuery(
       "from User where name = :username and password = :password")
       .setParameter("username", username)
       .setParameter("password", password)
       .getSingleResult();
       return true;
       }
       catch (NoResultException ex)
       {
       FacesMessages.instance().add("Invalid username/password");
       return false;
       }
       catch (Exception ex)
       {
       log.error("Caught", ex);
       FacesMessages.instance().add("Could not log in.. Please contact admin");
       return false;
       }
       }
      }
      


      It throws an exception saying:
      org.jboss.seam.RequiredException: In attribute requires value for component: authenticator.entityManager


      but if I change
      @In
       private EntityManager entityManager;


      to
      @PersistenceContext
       private EntityManager entityManager;


      I get a null pointer exception..

      Am I missing something, am I supposed to be outjecting the EntityManager somewhere?

      thanks,
      Dustin