Spring 选项 create-session="never" 在某些情况下被忽略?

Spring option create-session="never" is ignored in some scenarios?

对于某些 Web 服务,我想禁用 sessions。我在配置中添加了 create-session="never"

<beans:bean id="http403EntryPoint"
    class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint"/>
<http use-expressions="true" entry-point-ref="http403EntryPoint"
    create-session="never">
    <custom-filter ref="x509Filter" position="PRE_AUTH_FILTER"/>
</http>

这适用于大多数情况,除非 pre-authenticated 用户拥有未在应用程序中注册的客户端证书,因此我们的 AuthenticationUserDetailsS​​ervice 抛出 UsernameNotFoundException。 如果用户没有证书或有注册证书,则不会创建 session(在 HTTP 响应中没有 Set-Cookie header)。在所描述的情况下,将发送一个 cookie。它(cookie 分别是 session)也会根据每个后续请求进行评估,即使客户端证书已更改(基本上允许 session 固定攻击 - 应用程序使用保存的身份验证而不是 re-authenticating 每次调用)。

我们使用 Spring 安全 3.0.5。使用 Tomcat 6 和 7 以及 JBoss 7.1.1.

进行测试

为什么在描述的场景中创建 session?

PS:session 修复问题可能可以通过在 AbstractPreAuthenticatedProcessingFilter 中设置 checkForPrincipalChanges 来解决,但我对为什么要创建 session 的答案很感兴趣。

罪魁祸首是 https://jira.spring.io/browse/SEC-1476 :

在未经授权的情况下,class AbstractPreAuthenticatedProcessingFilter 中的以下方法将创建一个会话并将异常存储在那里:

protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) {
    SecurityContextHolder.clearContext();

    if (logger.isDebugEnabled()) {
        logger.debug("Cleared security context due to exception", failed);
    }
    request.getSession().setAttribute(WebAttributes.AUTHENTICATION_EXCEPTION, failed);
}

在修复中,他们更改了最后一行,删除了 getSession() 调用,因此 AuthenticationException 存储在请求中。

为了修复我们的项目,我创建了一个新的 class 扩展了 X509AuthenticationFilter 并且在那里我用相同的方法覆盖了方法 unsuccessfulAuthentication内容除了我还删除了 getSession() 调用。