我正在尝试在我的应用程序中实现注销,所以我做了这个:
public String logout(){
try{
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext ex = facesContext .getExternalContext();
ex.invalidateSession();
return "success";
}catch(Exception e){
return "error";
}
}
但是当我检查用户是否登录时,它说是:
public class AuthenticateListener implements PhaseListener {
@Override
public void afterPhase(PhaseEvent event) {
AuthorizedUser authorized = (AuthorizedUser) Util.getHandler("authorized");
if (authorized.getUser() == null) {
System.out.println("Not Logged");
} else {
System.out.println("Logged");
}
}
@Override
public void beforePhase(PhaseEvent event) {
// TODO Auto-generated method stub
}
@Override
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
}
我错过了什么吗?使我的会话无效后,我不应该获得 AuthorizedUser (sessionScoped) 的新实例吗?
编辑:添加 getHandler,如果有人需要它;)
public static Object getHandler(String handlerName) {
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ELResolver resolver = facesContext.getApplication().getELResolver();
Object uh = resolver.getValue(elContext, null, handlerName);
return uh;
}