在配置face-config.xml文件中这样配置
<lifecycle>
<phase-listener>
com.mobilecn.kys.login.filter.LoggedInCheck
</phase-listener>
</lifecycle>
实现类
package com.mobilecn.kys.login.filter;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
public class LoggedInCheck implements PhaseListener {
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
public void beforePhase(PhaseEvent event) {
System.out.println("beforePhasebeforePhasebeforePhasebeforePhasebeforePhasebeforePhasebeforePhasebeforePhasebeforePhasebeforePhasebeforePhase");
}
public void afterPhase(PhaseEvent event) {
System.out.println("afterPhaseafterPhaseafterPhaseafterPhaseafterPhaseafterPhaseafterPhaseafterPhaseafterPhaseafterPhase");
FacesContext fc = event.getFacesContext();
// Check to see if they are on the login page.
boolean loginPage = fc.getViewRoot().getViewId().lastIndexOf("login") > -1 ? true : false;
if (!loginPage && !loggedIn()) {
NavigationHandler nh = fc.getApplication().getNavigationHandler();
nh.handleNavigation(fc, null, "logout");
}
}
// 验证是否已经登陆
private boolean loggedIn() {
return LoginController.booleanValue();
}
}
package com.mobilecn.kys.login.filter;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginController {
/**
* 获得getFacesContext
*
* @return
*/
private static FacesContext getFacesContext() {
return FacesContext.getCurrentInstance();
}
/**
* 获得HttpServletRequest
*
* @return
*/
private static HttpServletRequest getHttpServletRequest() {
FacesContext facesContext = getFacesContext();
HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
return request;
}
/**
* 获得HttpServletResponse
*
* @return
*/
private static HttpServletResponse getHttpServletResponse() {
FacesContext facesContext = getFacesContext();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
return response;
}
public static boolean booleanValue(){
Object obj = getHttpServletRequest().getSession().getAttribute("userID");
if(obj == null || obj.equals("")){
return false;
}
return true;
}
}
<lifecycle>
<phase-listener>
com.mobilecn.kys.login.filter.LoggedInCheck
</phase-listener>
</lifecycle>
实现类
package com.mobilecn.kys.login.filter;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
public class LoggedInCheck implements PhaseListener {
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
public void beforePhase(PhaseEvent event) {
System.out.println("beforePhasebeforePhasebeforePhasebeforePhasebeforePhasebeforePhasebeforePhasebeforePhasebeforePhasebeforePhasebeforePhase");
}
public void afterPhase(PhaseEvent event) {
System.out.println("afterPhaseafterPhaseafterPhaseafterPhaseafterPhaseafterPhaseafterPhaseafterPhaseafterPhaseafterPhase");
FacesContext fc = event.getFacesContext();
// Check to see if they are on the login page.
boolean loginPage = fc.getViewRoot().getViewId().lastIndexOf("login") > -1 ? true : false;
if (!loginPage && !loggedIn()) {
NavigationHandler nh = fc.getApplication().getNavigationHandler();
nh.handleNavigation(fc, null, "logout");
}
}
// 验证是否已经登陆
private boolean loggedIn() {
return LoginController.booleanValue();
}
}
package com.mobilecn.kys.login.filter;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginController {
/**
* 获得getFacesContext
*
* @return
*/
private static FacesContext getFacesContext() {
return FacesContext.getCurrentInstance();
}
/**
* 获得HttpServletRequest
*
* @return
*/
private static HttpServletRequest getHttpServletRequest() {
FacesContext facesContext = getFacesContext();
HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
return request;
}
/**
* 获得HttpServletResponse
*
* @return
*/
private static HttpServletResponse getHttpServletResponse() {
FacesContext facesContext = getFacesContext();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
return response;
}
public static boolean booleanValue(){
Object obj = getHttpServletRequest().getSession().getAttribute("userID");
if(obj == null || obj.equals("")){
return false;
}
return true;
}
}