1 25 26 package enterprise.jsf_jpa_war; 27 28 import javax.el.ELContext; 29 import javax.el.ValueExpression; 30 import javax.faces.FacesException; 31 import javax.faces.context.ExternalContext; 32 import javax.faces.context.FacesContext; 33 import javax.faces.event.PhaseEvent; 34 import javax.faces.event.PhaseId; 35 import javax.faces.event.PhaseListener; 36 37 45 public class AuthenticationPhaseListener implements PhaseListener { 46 47 50 private static final String USER_LOGIN_OUTCOME = "login"; 51 52 54 62 public void afterPhase(PhaseEvent event) { 63 FacesContext context = event.getFacesContext(); 64 65 if (userExists(context)) { 66 return; 68 } else { 69 if (requestingSecureView(context)) { 71 context.responseComplete(); 72 context.getApplication(). 73 getNavigationHandler().handleNavigation(context, 74 null, 75 USER_LOGIN_OUTCOME); 76 } 77 } 78 } 79 80 83 public void beforePhase(PhaseEvent event) { 84 } 85 86 89 public PhaseId getPhaseId() { 90 return PhaseId.RESTORE_VIEW; 91 } 92 93 95 103 private boolean userExists(FacesContext context) { 104 ExternalContext extContext = context.getExternalContext(); 105 return (extContext.getSessionMap().containsKey(UserManager.USER_SESSION_KEY)); 106 } 107 108 119 private boolean requestingSecureView(FacesContext context) { 120 ExternalContext extContext = context.getExternalContext(); 121 String path = extContext.getRequestPathInfo(); 122 return (!"/login.jsp".equals(path) && !"/create.jsp".equals(path)); 123 } 124 } 125 | Popular Tags |