簡體   English   中英

Spring Security記住我不工作

[英]Spring Security Remember Me not working

我已經花了一天時間就可以了,但不明白為什么remember_me在Spring Security是行不通的。 我只使用xml作為配置,沒有Java代碼:

<http use-expressions="false" auto-config="true">
        <intercept-url pattern="/css/**" filters="none" />
        <intercept-url pattern="/**" access="ROLE_USER" />
        <form-login login-page="/BL/index.html" default-target-url="/BL/main.html"
            authentication-failure-url="/BL/index.html?autherror=true"
            always-use-default-target='true' />
        <logout logout-url="/logout" logout-success-url="/BL/index.html"
            invalidate-session="true" />
        <remember-me data-source-ref="dataSource"
            user-service-ref="jdbcUserService" key="99999989"
            token-validity-seconds="86400" />
        <access-denied-handler error-page="/BL/index.html?autherror=true" />
    </http>

簡而言之, index.html是我的登錄頁面。 登錄時,我看到一個名為SPRING_SECURITY_REMEMBER_ME_COOKIE的cookie正在創建,其值為V2FrblE1Vlk3cFd6ZDIvZnRjdnlIZz09OndOb2h2NmUvdFlZRHJvRFRPV3lheHc9PQ

但是,如果現在返回index.html頁面,它將要求我再次登錄。 我仍然看到相同的Cookie值。 上面的xml配置有什么錯誤? 我希望它不應該要求我再次登錄,而應該重定向到我的主頁,因為我已經登錄了。

我看到以下登錄服務器。 看起來正在與用戶一起以空字符串發送請求:

14:16:34,031 DEBUG JdbcUserDetailsManager:155-查詢未返回用戶結果''14:16:34,031 DEBUG DaoAuthenticationProvider:131-用戶''未找到14:16:34,031 DEBUG UsernamePasswordAuthenticationFilter:318-身份驗證請求失敗:org.springframework .security.authentication.BadCredentialsException:錯誤的憑據14:16:34,032 DEBUG UsernamePasswordAuthenticationFilter:319-更新了SecurityContextHolder以包含空身份驗證14:16:34,032 DEBUG UsernamePasswordAuthenticationFilter:320-委派身份驗證失敗處理程序org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler @ 6011c7cf 14:16:34,032調試PersistentTokenBasedRememberMeServices:214-交互式登錄嘗試未成功。 14:16:34,032 DEBUG PersistentTokenBasedRememberMeServices:300-取消Cookie 14:16:34,032 DEBUG SimpleUrlAuthenticationFailureHandler:67-重定向到/BL/index.html?autherror=true

Spring不會阻止已登錄的用戶訪問登錄頁面。

要測試RememberMe提供程序,您至少需要兩頁(具有不同的登錄名)

  • 登錄頁面
  • 普通頁面,只有在用戶登錄后才能訪問。

用戶擁有此cookie后,可以關閉瀏覽器,再次打開瀏覽器,然后直接訪問受保護的頁面(不應將用戶重定向或轉移到登錄頁面)

但是您的日志聲明顯示,您嘗試通過登錄頁面登錄用戶,另一方面,您告訴您要使用“記住我”令牌。 - 要使用“記住我的Cookie”,您必須不要求登錄頁面,而要請求一些普通頁面。

RememberMeAuthenticationFilter將(在嘗試登錄時)在DEBUG級別上顯示以下消息:

  • “用我記住的令牌填充了SecurityContextHolder:...”(成功)
  • “ SecurityContextHolder未使用“記住我”令牌填充,因為AuthenticationManager拒絕了RememberMeServices返回的身份驗證...”(失敗)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM