注销后的JSF生命周期



我使用基于表单的身份验证。

我有一个注销链接,看起来像:

<h:commandLink action="#{loginBean.logout}">
    <h:outputText value="logout" />
</h:commandLink></div>

和相应的注销方法:

public String logout() {
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    return "/view/index?faces-redirect=true"; // Redirect added as per BalusC's suggestion.
}

点击登出链接后,我返回到首页,但似乎没有CSS。当我点击按钮运行搜索时,我得到以下错误:

javax.faces.application.ViewExpiredException: viewId:/view/index.jsf - View /view/index.jsf could not be restored.

CSS实际上是在/resources下面根据我对web。xml的理解,它不需要认证:

    <security-constraint>
    <web-resource-collection>
        <web-resource-name>fizio</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>*</role-name>
    </auth-constraint>
</security-constraint>
<security-constraint>
    <web-resource-collection>
        <web-resource-name>Unprotected area</web-resource-name>
        <url-pattern>/resources/*</url-pattern>
    </web-resource-collection>
</security-constraint>

从这个状态,我似乎能够再次登录,看到一些数据之间偶尔的视图无法恢复错误,但没有CSS。这一切都有点支离破碎。如有任何建议,不胜感激。

ETA:登录表单:

<form method="POST" action="j_security_check">
    <label for="j_password">Username:</label> <input type="text" name="j_username" />
    <br />
    <label for="j_password">Password:</label> <input type="password" name="j_password" /> <input type="submit" value="Login" />
</form>

无效后需要重定向。否则,页面将在"无效"会话中显示。将faces-redirect=true添加到结果中以触发重定向。

public String logout() {
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    return "/index?faces-redirect=true";
}

重定向将导致浏览器在POST响应后触发一个新的GET请求,从而导致服务器创建一个全新的会话。这样,视图将按预期工作。

至于CSS资源,它们显然仍然需要登录。"无保护区域"的限制是行不通的。删除它并将您的主要安全约束的url模式更改为例如/app/*或受保护区域的任何公共路径。

最新更新