I'm using JBoss 7.1.1, JSF2.0, EJB 3.1.

I'm trying to auto login a user when re-visiting. (Not a full login, but at least I give him his customized views - I would never let him/her invoke a financial transaction based on that cookie identification).

So, I thought it's easy to grab a JSF phase listener, check for the previously set cookie (containing a UUID) and auto login that user. I quickly noticed, that I could not inject beans.

Hm. Ok. But how else would you establish auto login? It certainly must be at a place where every request goes thru - no mather of the URI requested.

So, my initial approach was the code below - not working of course, since all injected objects are set to null. 

1) Any idea how to accomplish this?  

2) This looks like a costly computation (thinking of every request going thruw this phase listener). A smarter way that imposes?

```
public class ReLoginPhaseListener implements PhaseListener {

private static final long serialVersionUID = 3690040902641689160L;
@Inject
private Logger log;

@Inject
private FacesContext ctx;

@Inject 
BeanManager beanMgr;

/**
 * The existing loginController (as a managed property since it's a managed
 * bean.)
 */
@ManagedProperty(value = "#{loginController}")
private LoginController loginController;

/*
 * Cannot inject a EJB here, because it it's not managed by the JSF engine.
 * @Inject UserRepository userRepository;
 */
@Override
public void afterPhase(PhaseEvent event) {
    // No need to implement
}

@Override
public void beforePhase(PhaseEvent event) {
    // check if is in session????
    FacesContext ctx = event.getFacesContext();
    Map<String, Object> cookies = ctx.getExternalContext().getRequestCookieMap();
    Cookie rmCookie = (Cookie) cookies.get("cCode");
    Cookie userCookie = (Cookie) cookies.get("userid");
    // Only go further if cCode exists
    if (rmCookie != null && userCookie != null) {
        Map<String, Object> sessionMap = ctx.getExternalContext().getSessionMap();
        User currentUser = (User) sessionMap.get("user");
        if (currentUser != null) {
            String rmKey = currentUser.getRememberMeKey();
            // If the remember key exists, then check if it corresponds to
            // the cookie code.
            if (rmKey != null && !rmKey.isEmpty()) {

            }
        } else {
            // Read from the database if there's a user with this id and
            // cCode.
            UserRepository userRep = getUserRepFacade();
            User user = userRep.findById(Long.parseLong(userCookie.getValue()));
            if (user.getRememberMeKey().equals(rmCookie.getValue()))
                loginController.setUser(user);
        }
    }
    // get request object and check if cookie is available. if corresponds
    // to the database, then do a restricted-log in.
}

@Override
public PhaseId getPhaseId() {
    // TODO Auto-generated method stub
    return PhaseId.RESTORE_VIEW;
}

public UserRepository getUserRepFacade() {      
    Bean<UserRepository> bean = (Bean<UserRepository>) beanMgr.getBeans(UserRepository.class)
            .iterator().next();
    CreationalContext<UserRepository> ctx = beanMgr.createCreationalContext(bean);
    UserRepository userRep = (UserRepository) beanMgr.getReference(bean, UserRepository.class, ctx);
    // this could be inlined, but intentionally left this way
    return userRep;
}

```

}