2011-02-04 43 views
9

在我的JSF应用程序中,我得到的用户这样当前登录的名字......使用session.invalidate的JSF注销不会清除当前的用户名?

public String getLoggedInUsername() { 
    return FacesContext.getCurrentInstance().getExternalContext().getRemoteUser(); 
} 

...我检查用户是否喜欢这个签署...

public boolean isSignedIn() { 
    return (getLoggedInUsername() != null); 
} 

...当用户登出后,我做到这一点...

public String doLogout() { 
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    HttpSession httpSession = (HttpSession)facesContext.getExternalContext().getSession(false); 
    httpSession.invalidate(); 
    return "main"; 
} 

我的问题是doLogout()时,getLoggedInUsername()仍然返回在登录的用户名之后。什么我应该这样做,以确保注销后getRemoteUser()返回null?

另外,是否有更好的方式来获得如果isSignedIn()比只检查用户名?

谢谢! Rob

+0

您是否调试过涉及的方法本身,或者您是否基于Web浏览器的行为方式自己猜测/归纳它?该页面可能是从浏览器缓存中的普通请求... – BalusC 2011-02-04 15:10:48

回答

16

确保在无效会话后重定向请求。用户主体根据会话属性进行解析。所以,当你只是转发到目标页面(默认情况下,作为JSF导航的情况下),那么它仍然会在目标页面中,因为它使用相同的HttpSession参考。重定向指示网页浏览器发出全新的HTTP请求,由此迫使服务器基于新请求重新创建参考文献HttpSession

<redirect/>添加到导航案例以强制JSF发送重定向。或者当您已经在JSF 2.0中时,将?faces-redirect=true添加到结果值。

0

调用“session.invalidate();” ,添加“request.logout();”。

会话对象的方法无效只是清理会话数据。

请求对象上的方法注销将getUserPrincipal,getRemoteUser和getAuthType调用请求时返回的值设置为null。

相关问题