gpt4 book ai didi

java - 注销后的 JSF 生命

转载 作者:行者123 更新时间:2023-11-29 06:10:19 29 4
gpt4 key购买 nike

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

我有一个注销链接,如下所示:

<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。真的有点破了。如有任何建议,我们将不胜感激。

预计到达时间:登录表单:

<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/* 或安全区域的任何通用路径。

关于java - 注销后的 JSF 生命,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7148290/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com