1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package enterprise.jsf_jpa_war;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
public class AuthenticationPhaseListener implements PhaseListener {
private static final String USER_LOGIN_OUTCOME = "login";
public void afterPhase(PhaseEvent event) {
FacesContext context = event.getFacesContext();
if (userExists(context)) {
// allow processing of the requested view
return;
} else {
// send the user to the login view
if (requestingSecureView(context)) {
context.responseComplete();
context.getApplication().
getNavigationHandler().handleNavigation(context,
null,
USER_LOGIN_OUTCOME);
}
}
}
public void beforePhase(PhaseEvent event) {
}
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
private boolean userExists(FacesContext context) {
ExternalContext extContext = context.getExternalContext();
return (extContext.getSessionMap().containsKey(UserManager.USER_SESSION_KEY));
}
private boolean requestingSecureView(FacesContext context) {
ExternalContext extContext = context.getExternalContext();
String path = extContext.getRequestPathInfo();
return (!"/login.jsp".equals(path) && !"/create.jsp".equals(path));
}
} |
Partager