4

我正在使用 spring 记住我的服务。我看到一个奇怪的行为。

我正在采取的步骤:

  • 使用用户名/密码登录我的网站并选中记住我复选框

  • 比我关闭浏览器并打开一个新浏览器并打开一个新浏览器。从技术上讲,我必须自动登录。这也在发生。我正在使用PersistentTokenBasedRememberMeServices.

  • 现在我再次关闭浏览器并再次打开一个新浏览器,当我尝试访问我的网站时,出现以下异常:

        SEVERE: Servlet.service() for servlet [appServlet] in context with path [/Spring-Security] threw exception org.springframework.security.web.authentication.rememberme.CookieTheftException: Invalid remember-me token (Series/token) mismatch. Implies previous cookie theft attack.
        at org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices.processAutoLoginCookie(PersistentTokenBasedRememberMeServices.java:102)
        at org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices.autoLogin(AbstractRememberMeServices.java:115)
        at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:97)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    

    我深入研究,我看到我的最后两个请求记住我令牌SPRING_SECURITY_REMEMBER_ME_COOKIE的值是

    bUpwUUJ3dGpUcVJjaGpIYXJxcmFkdz09OlBBRlZXbDVnYmZZQjM2RmFYVDNVMXc9PQ

但是弹簧解码了(org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices.tokenRepository)

    final String presentedSeries = cookieTokens[0];
    final String presentedToken = cookieTokens[1];

在第一个请求中正确,但在第二个请求中,它解码了 presentToken 一些不同的东西,导致 cookie 盗窃异常。

这是正确的行为吗?我该如何解决这个问题?

4

1 回答 1

0

我有这个完全相同的问题。第一个请求与令牌匹配,然后创建一个新令牌,但是在此之后,第二个请求将立即到达(与第一个请求具有相同的令牌)并且令牌将不再匹配!

我无法弄清楚为什么第二个请求会在第一个请求之后立即出现。

最后,我的解决方案是PersistentTokenBasedRememberMeServices继承并覆盖该processAutoLoginCookie方法。我只是注释掉了对 cookie 盗窃异常的检查(这会产生一个安全漏洞,但它对于我的目的来说是可以接受的)

如果您想要一个更安全的解决方案,您可以轻松地添加逻辑以匹配最后两个呈现的令牌。

于 2013-03-06T15:54:07.223 回答