【问题标题】:How to reuse JSF Beans of SessionScope across HTTP and HTTPS requests?如何跨 HTTP 和 HTTPS 请求重用 SessionScope 的 JSF Bean?
【发布时间】:2015-07-20 12:44:35
【问题描述】:

考虑这个 index.xhtml 页面。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html">
    <f:view locale="en" encoding="UTF-8">
        <h:head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        </h:head>

        <h:body class="ui-grid" id="body">
            <h:form>
                <h:commandButton value="Invalidate Session" action="#{loggedInUser.invalidateSession}"/>
            </h:form>
        </h:body>
    </f:view>
</html>

还有这个 SessionScoped bean。

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

@Named
@SessionScoped
public class LoggedInUser implements Serializable {

    String user;

    @PostConstruct
    public void init() {
        String remoteUser = this.user = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
        String sessionId = FacesContext.getCurrentInstance().getExternalContext().getSessionId(false);
        System.out.println("Initializing. With SESSION ID " + sessionId);
        System.out.println("Initializing. With remote user " + remoteUser);
    }

    public void invalidateSession() throws ServletException{
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        request.logout();
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }
}

我在两个浏览器窗口中访问该页面。 一个窗口 (W1) 的 URL 为 https://localhost:8181/pfdialogdemo/index.xhtml 另一个窗口 (W2) 的 URL 为 http://localhost:8080/pfdialogdemo/index.xhtml

这些是我要做的步骤。

  1. 在 W1 上单击“使会话无效”。
  2. 重新加载 W1。
  3. 重新加载 W2。

LoggedInUser.init() 产生的输出;是

Information:   Initializing. With SESSION ID b6d2858cc441c52540f54ee4cb0c  
Information:   Initializing. With remote user null 
Information:   Initializing. With SESSION ID b6d60472f8d5cca5b6feb5ff32d5 
Information:   Initializing. With remote user null

如果我这样做了。

  1. 在 W2 上单击“使会话无效”。
  2. 重新加载 W2。
  3. 重新加载 W1。

输出是。

Information:   Initializing. With SESSION ID b6de615078648aa44566af533a9f
Information:   Initializing. With remote user null

为什么在第一个示例中第二次创建了 SessionScoped bean?如何确保 SessionScoped bean 没有被创建两次?为什么第二个例子只创建了一次?

此示例使用 Glassfish 4.1 和 JSF 2.2 (Mojarra)

【问题讨论】:

    标签: http jsf-2 https


    【解决方案1】:

    这是符合HTTP state (cookie) specification 的预期行为(即超出 JSF 的控制范围)。

    在 HTTPS 请求期间创建的 Cookie 不适用于同一域/路径上的 HTTP 请求。否则,这将是一个安全漏洞,破坏了 HTTPS 的完整性和意义。只有反过来才有可能。

    只需从 HTTP 永久重定向到 HTTPS,即可在您的 Web 应用程序上禁用 HTTP。

    【讨论】:

    • 感谢您的回答。如何在 JSF 应用程序中从 HTTP 永久重定向到 HTTPS?
    • 这是正确的问题:“如何在 GlassFish 服务器中从 HTTP 永久重定向到 HTTPS?”。
    猜你喜欢
    • 2017-04-24
    • 2015-06-25
    • 2013-11-02
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    相关资源
    最近更新 更多