Hi Caye,

This is my authenticator bean:

@Name("authenticator")
  | @Stateless
  | public class AuthenticatorBean implements Authenticator {
  | 
  |     @In(required=false) @Out(required=false)
  |     UserLogin userLogin;
  |     
  |     @In Identity identity;
  | 
  |     @In(create=true) FacesMessages facesMessages;
  |     
  |     @PersistenceContext EntityManager entityManager;
  | 
  | 
  |     public boolean authenticate()
  |     {
  |             String pwd = Passwords.hashPassword(identity.getPassword());
  |         
  |         try {
  |             User user = (User)entityManager.createQuery("from User u where 
u.login = :login " +
  |                                                     "and upper(u.password) 
= :pwd")
  |                                                     .setParameter("login", 
identity.getUsername().toUpperCase())
  |                                                     .setParameter("pwd", 
pwd.toUpperCase())
  |                                                     .getSingleResult();
  | 
  |             if (user.getRoles().size() == 0) {
  |                     facesMessages.addFromResourceBundle("login.norole");
  |                     return false;
  |             }
  |             
  |             
  |             // saves login info
  |             FacesContext facesContext = FacesContext.getCurrentInstance();
  |             HttpServletRequest req = (HttpServletRequest) 
facesContext.getExternalContext().getRequest();
  |             String ipAddr = req.getRemoteAddr();
  |             String app = req.getHeader("User-Agent");
  | 
  |             userLogin = new UserLogin();
  |             userLogin.setUser(user);
  |             userLogin.setLoginDate(new java.util.Date());
  |             if (app.length() > 80)
  |                     app = app.substring(0, 80);
  |             userLogin.setApplication(app);
  |             userLogin.setIpAddress(ipAddr);
  |             entityManager.persist(userLogin);
  | 
  |             for (UserRole r: userLogin.getUser().getRoles()) {
  |                     identity.addRole(r.getName());
  |             }
  |             
  |             return true;
  |         }
  |         catch (NoResultException e) {
  |             e.printStackTrace();
  |             return false;           
  |         }
  |     }
  | 
  | 
  |     public void logout() {
  |         if ((userLogin != null)&&(userLogin.getLogoutDate() == null)) {
  |             userLogin = entityManager.merge(userLogin);
  |             userLogin.setLogoutDate(new java.util.Date());
  | 
  |             entityManager.persist(userLogin);
  | 
  |             System.out.println("Logout de " + 
userLogin.getUser().getLogin());
  |         }
  |     }
  | }

Check that I had to create it as a Stateless bean because of the EntityManager.

Regards,
Ricardo

View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4068954#4068954

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4068954
_______________________________________________
jboss-user mailing list
[email protected]
https://lists.jboss.org/mailman/listinfo/jboss-user

Reply via email to