注销时清除客户端SSL状态(使用双向SSL)

时间:2013-02-21 14:36:16

标签: java-ee jsf-2 ssl-certificate

我正在开发一个需要双向身份验证才能识别服务器用户的项目。然后,服务器根据用户的身份显示该用户可用的信息和选项。

我在主模板上放了一个“退出”链接。这样做的目的是使一切无效/清除,并将它们重定向到一个非安全的页面,上面写着“你已经退出”。使用“登录”按钮将其重定向回登录页面。

登录页面应该提示用户输入他们的证书(这是第一次)。但是,当您单击“签名”页面上的“登录”按钮时,它将使用上一个用户的SSL状态,并且不会出现证书提示。

我真正需要的是一个注销方法(不想使用java脚本),它将清除所有缓存,使所有会话对象无效,并清除SSL状态。

以下是我到目前为止所做的事情:

1。)在web.xml中设置一个安全约束,强制任何页面匹配模式'/ secure / *'只能通过https访问。这很有效......

2.。)创建了一个阻止缓存的过滤器......

3.。)创建了一个退出的功能......

非常感谢任何和所有帮助!

过滤代码:

@WebFilter("/secure/*")
public class NoCacheFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
        httpResponse.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
        httpResponse.setHeader("Pragma", "no-cache");
        httpResponse.setDateHeader("Expires", 0L); // Proxies.
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {}
}

退出功能

public void signOut() {
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();

    // Log Out of the Session
    request.logout();

    // Invalidate the Session objects
    externalContext.invalidateSession();
    if (externalContext.getRequestCookieMap().get(OPEN_TOKEN_COOKIE) != null) {
        externalContext.addResponseCookie(OPEN_TOKEN_COOKIE,null,Collections.<String, Object>singletonMap("maxAge", 0));
    }

    // Redirect to the Sign out page
    String signOutURL = MessageFormat.format("http://{0}{1}/{2}",externalContext.getRequestServerName(),externalContext.getRequestContextPath(),"goodbye.jsf");
    externalContext.redirect(signOutURL);
}

----------------------编辑----------------------

我知道我说过我不想使用JavaScript,但我会在找到更好的解决方案之前,我会分享我是如何与你一起完成的。

我正在使用PrimeFaces按钮,其操作指向之前的signOut函数引用。 oncomplete参数指向一个清除Internet Explorer和Firefox的SSL状态的JavaScript(还没有尝试过其他人)。

退出按钮

<p:commandButton value="Sign out" action="#{adminNavigationBean.signOut}" oncomplete="clearSSLState();" styleClass="commandButton"/>

JavaScript clearSSLState()

<script type="text/javascript">
    function clearSSLState() {
        document.execCommand("ClearAuthenticationCache");
        window.crypto.logout();
    }
</script>

请注意,我的goodbye.xhtml页面不在我的/ secure /文件夹中。如果是,则会立即提示用户选择证书。这是由web.xml中的security-constraint引起的,并且是正确的行为。

web.xml security-constraint

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Protected Context</web-resource-name>
        <url-pattern>/secure/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>

希望这有助于其他人。

0 个答案:

没有答案