【问题标题】:Logout JSF, using SSL in Glassfish 3注销 JSF,在 Glassfish 3 中使用 SSL
【发布时间】:2011-08-01 09:00:18
【问题描述】:

我还没有找到这个问题的真正解决方案,即使它真的很常见......

我有这样的背景:

  • 在 Glassfish Server v3.1、JDK6 上运行的 JSF 应用程序。全部在我的带有 WinVista 的个人计算机中(最后一个应该不重要)。
  • 使用 SSL 和基本身份验证(容器的安全性)
  • 我在后台 bean 中完成了 logout() 方法,使会话无效并发送重定向。

我不能让容器再次显示登录框来验证用户,并且能够更改用户......我的用户总是可以返回,在浏览器中按 BACK 按钮或只写 URL,并在假定不应该存在现有会话时继续在那里进行操作。

我正在获取创建我的支持 bean 的用户的名称:

private void setName() {
    this.name = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName();

}

而且我用这个名字来执行操作……

然后注销我的 xhtml 有代码:

        <h:panelGroup id="logOut">
        <h:form>
            <h:commandLink id="linkLogOut" action="#{visitor.logout}" value="  Clic here to Log Out" />
        </h:form>
    </h:panelGroup>

在我的 bean 中调用这个方法::

public void logout() throws IOException {
   // FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    this.name = null;
    FacesContext fc = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession)fc.getExternalContext().getSession(false);
    session.invalidate();
FacesContext.getCurrentInstance().getExternalContext().redirect("https://localhost:8080/");
}

我声明我的支持 bean:

@Named(value="visitor")
@SessionScoped

...我也在从部署描述符中进行重定向...而且是一样的。

如果我关闭浏览器,会话将丢失,并且容器会再次向我询问用户/密码。

有什么建议吗?

非常感谢!

亚历杭德罗。

【问题讨论】:

    标签: jsf logout


    【解决方案1】:

    我无法让容器再次显示登录框以验证用户,并且能够更改用户...而且我的用户总是可以返回,按下浏览器中的 BACK 按钮或只是写URL,并在假定不应该存在现有会话时继续在那里进行操作。

    这些页面可能是从浏览器缓存中显示的,而不是从服务器重新请求的。要阻止这种情况,请创建一个 Filter,它映射到感兴趣的 URL 模式(*.jsf 可能吗?)并在 doFilter() 方法中执行以下工作:

    HttpServletResponse httpResponse = (HttpServletResponse) response;
    httpResponse.setHeader("Cache-Control", "no-cache,no-store,must-revalidate"); // HTTP 1.1
    httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0
    httpResponse.setDateHeader("Expires", 0); // Proxies.
    chain.doFilter(request, response);
    

    测试前清除浏览器缓存。

    【讨论】:

      【解决方案2】:

      非常感谢您的所有回答。

      我请了几天假...并没有回复这个帖子。

      我只想分享我的解决方案。

      基本认证是绑定在浏览器上的,一旦你完成登录,就应该关闭浏览器才能完成注销......这不是那么优雅。

      我更改为 FORM 身份验证,创建我的表单,并将用户信息发布到一个 servlet,该 servlet 针对我的领域验证用户凭据,这个 servlet 将用户重定向到应用程序或错误页面。显然,必须在 web.xml 中正确配置 ifor security-constrain。

      我在这里分享我的解决方案:

      登录页面:

      <!-- This is the login page that implement the "Form Base" login -->
      <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html">
      <h:head>
          <title>Login Form</title>
      </h:head>
      <h:body>
          <h2>Welcome to the secure messaging service!:</h2>
          <form name="loginForm" method="POST" action="j_security_check">
              <p><strong>Please type your user name: </strong>
                  <input type="text" name="j_username" size="25" /></p>
              <p><strong>Please type your password: </strong>
                  <input type="password" size="15" name="j_password" /></p>
              <p>
                  <input type="submit" value="Submit"/>
                  <input type="reset" value="Reset"/>
              </p>
          </form>
      </h:body>
      

      我的验证 servlet 方法:

      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      try {
              String username;
              String password;
              username = request.getParameter("j_username").toString();
              password = request.getParameter("j_password").toString();
              request.login(username, password);
              response.sendRedirect("/SecureMessageWebInterface/");
          } catch (Exception e) {
              response.sendRedirect("error.xhtml");
          }           
      }
      

      以及配置文件所需的部分......

      <security-constraint>
          <display-name>Web Interface</display-name>
          <web-resource-collection>
              <web-resource-name>SSL Pages</web-resource-name>
              <description/>
              <url-pattern>/</url-pattern>
          </web-resource-collection>
          <auth-constraint>
              <description>All site is restricted</description>
              <role-name>user</role-name>
          </auth-constraint>
          <user-data-constraint>
              <description>Secure connection is required</description>
              <transport-guarantee>CONFIDENTIAL</transport-guarantee>
          </user-data-constraint>
      </security-constraint>
      <login-config>
          <auth-method>FORM</auth-method>
          <realm-name>webapps</realm-name>
          <form-login-config>
              <form-login-page>/login.xhtml</form-login-page>
              <form-error-page>/error.xhtml</form-error-page>
          </form-login-config>
      </login-config>
      

      我希望这对其他人有用。

      一切顺利,

      亚历杭德罗。

      【讨论】:

        猜你喜欢
        • 2014-02-14
        • 2011-12-17
        • 1970-01-01
        • 2011-09-22
        • 1970-01-01
        • 1970-01-01
        • 2013-01-21
        • 2012-07-26
        • 1970-01-01
        相关资源
        最近更新 更多