我正在使用 JBoss 7.1.1、JSF2.0、EJB 3.1。
我试图在重新访问时自动登录用户。(不是完全登录,但至少我给了他他的自定义视图——我永远不会让他/她根据那个 cookie 标识调用金融交易)。
因此,我认为获取 JSF 阶段侦听器、检查先前设置的 cookie(包含 UUID)并自动登录该用户很容易。我很快注意到,我不能注射豆子。
嗯。好的。但是,您将如何建立自动登录?它肯定必须在每个请求都通过的地方——与请求的 URI 无关。
所以,我最初的方法是下面的代码——当然不起作用,因为所有注入的对象都设置为空。
1)知道如何做到这一点吗?
2)这看起来像一个昂贵的计算(考虑到每个请求都会通过这个阶段监听器)。一个更聪明的方式强加?
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;
}
}