有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Rolling Memory me cookies与Spring Security

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

我现在正在寻找一种使用TokenBasedMemberMeservices来延长每次成功登录(交互式或非交互式)后的时间的方法。我想在过滤器中添加一个AuthenticationSuccessHandler,或者在同一个类中重写onSuccessfulAuthentication,但我很好奇a)你们中是否有人遇到了同样的问题,b)为什么这不是MemberMemberServices的内置选项

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


共 (2) 个答案

  1. # 1 楼答案

    当我阅读^{}的代码以给出替代答案时,我发现这行代码:

     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,它描述了以下功能:

    I've added support for this to TokenBasedRememberMeServices. It allows the use of a negative value as the tokenValiditySeconds property. If the value is negative, the token expiryTime (as used in the signature) will remain at the default of 14 days, but the cookie maxAge will be set to the negative value, preventing it from being persisted on the client when the browser closes.

    PersistentTokenBasedRememberMeServices will reject a negative value on initialization.

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

  2. # 2 楼答案

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