【问题标题】:Rolling remember me cookies with Spring Security使用 Spring Security 滚动记住我的 cookie
【发布时间】:2014-10-04 11:03:14
【问题描述】:

我正在使用 Spring 的 TokenBasedRememberMeServicesRememberMeAuthenticationFilter 通过 Base-64 编码的 cookie 来识别以前记住的用户。根据接口合同,cookie 只会在每次 interactive 登录后设置/更新。这意味着 cookie 的 TTL 不会在每次成功的基于 cookie 的登录后更新。

我现在正在寻找一种方法来使用 TokenBasedRememberMeServices 来延长每次成功登录后的时间段(交互式或非交互式)。我正在考虑在过滤器中添加AuthenticationSuccessHandler,或者在同一个类中覆盖onSuccessfulAuthentication,但我很好奇a)你们中的任何人是否遇到过同样的问题,b)为什么这不是内置选项记住我的服务。

PS:“滚动记住我的 cookie”具有明显的安全缺陷,因为一个 cookie 基本上可以让您在不知道密码的情况下永远登录,但我们先把这个放在一边。

【问题讨论】:

    标签: java spring cookies spring-security


    【解决方案1】:

    我相信您已经完全回答了自己的问题。处理它的正确方法是实现AuthenticationSuccessHandler。这是成功后执行自定义逻辑的正确位置。您还解释了为什么很少遇到这种情况,同时,为什么没有开箱即用的解决方案。一方面,我不能说我在网络上看到过这种行为。

    【讨论】:

    • 嗯?很多网站都有这个,包括 StackOverflow。
    【解决方案2】:

    当我阅读TokenBasedRememberMeServices 的代码以给出替代答案时,我发现了这一行:

     public void onLoginSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication successfulAuthentication) {
        int tokenLifetime = calculateLoginLifetime(request, successfulAuthentication);
        long expiryTime = System.currentTimeMillis();
        // SEC-949
        expiryTime += 1000L* (tokenLifetime < 0 ? TWO_WEEKS_S : tokenLifetime);
    
        String signatureValue = makeTokenSignature(expiryTime, username, password);
    
        setCookie(new String[] {username, Long.toString(expiryTime), signatureValue}, tokenLifetime, request, response);
    

    如您所见,每次用户成功登录时,都可以通过将tokenLifetime 设置为负值来延长 cookie 的生命周期。

    如果您看到SEC-949,它描述了功能:

    我已向 TokenBasedRememberMeServices 添加了对此的支持。它允许 使用负值作为 tokenValiditySeconds 属性。如果 该值为负数,令牌过期时间(在签名中使用) 将保持默认的 14 天,但 cookie maxAge 将是 设置为负值,防止它被持久化 浏览器关闭时的客户端。

    PersistentTokenBasedRememberMeServices 将在初始化时拒绝负值。

    如果你想要更多的定制,我想你需要扩展类,或者修改类并将补丁发送到 spring。

    【讨论】:

    • 你确定它可以延长寿命吗?从我的角度来看,它只是在关闭浏览器后删除了 cookie。 onLoginSuccess 仍然只在交互式登录后被调用,不是吗?
    猜你喜欢
    • 2015-05-10
    • 2012-01-21
    • 2018-12-02
    • 2015-06-12
    • 2014-09-04
    • 2012-03-13
    • 1970-01-01
    • 2013-04-09
    • 2017-05-12
    相关资源
    最近更新 更多