我正在使用基于表单的身份验证。
我有一个注销链接,如下所示:
<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>
在这种状态下,我似乎能够再次登录并在偶尔的 View 无法恢复错误之间看到一些数据,但看不到CSS。这一切真的有点 splinter 了。如有任何建议,我们将不胜感激。
预计到达时间:登录表单:
<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>
最佳答案
无效后需要重定向。否则,该页面将在“无效” session 中显示。将 faces-redirect=true
添加到结果中以触发重定向。
public String logout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "/index?faces-redirect=true";
}
重定向将导致网络浏览器在 POST 响应后触发新的 GET 请求,进而导致服务器创建一个全新的 session 。这样 View 就会按预期工作。
至于CSS资源,他们显然仍然需要登录。你那里的“未 protected 区域”限制是行不通的。删除它并将主要安全约束的 URL 模式更改为例如 /app/*
或安全区域的任何公共(public)路径。
关于java - 注销后的 JSF 生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7148290/