java - 注销后的 JSF 生命周期

标签 java jboss jsf-2 logout

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

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

<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/

相关文章:

css - JSF 2.x 在空时隐藏消息层

java - FacesContext.getCurrentInstance() 在 FacesServlet(?) 处理的请求中返回 null

java - 提交在 <ui :repeat> 内不起作用

java - Sql Server数据库中的uniqueidentifier - 如何在Java环境中生成?

java - 如何使用 HttpComponents (Java apache lib) 发出请求,更改 TLS 握手中的服务器名称指示

java - 当 charAt 和 String.valueOf 的值相同时,为什么它们的比较不返回 true?

java - Wildfly jms配置

java - 如何应用 Hibernate 补丁

jboss - 将 netty 与 openshift 集成的最佳方法

java - 在 Spring Boot 2 中使用实体管理器时,没有类型 'javax.persistence.EntityManager' 的合格 bean