2014-01-29 100 views
0

我已经花了一天的时间,但不明白为什么remember_me在SPring安全不起作用。我只使用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:34031 DEBUG JdbcUserDetailsManager:155 - 查询返回任何 结果为用户 '' 14:16:34031 DEBUG DaoAuthenticationProvider还可以为:131 - 用户'' 未找到14:16:34031 DEBUG UsernamePasswordAuthenticationFilter:318 - 身份验证请求 失败: org.springframework.security.authentication.BadCredentialsException: 认证错误14:16:34032 DEBUG UsernamePasswordAuthenticationFilter:319 - 更新 SecurityContextHolder中包含null身份验证14:16:34,032 DE BUG UsernamePasswordAuthenticationFilter:320 - 委派到 认证失败 handlerorg.springframework.se[email protected]6011c7cf 14:16:34032 DEBUG对PersistentTokenBasedRememberMeServices:214 - 交互式登录尝试是不成功的。 14:16:34032 DEBUG 对PersistentTokenBasedRememberMeServices:300 - 取消饼干 14:16:34032 DEBUG SimpleUrlAuthenticationFailureHandler:67 - 重定向到/BL/index.html?autherror=true

+0

你使用哪种版本的spring security?当用户使用该用户名和密码登录时, – Ralph

+0

是存储在数据库中的令牌吗? – Ralph

+0

@Ralph Spring-Security 3.0.4 – Jatin

回答

6

春天并不妨碍在登录用户访问登录页面。

测试了rememberMe提供至少需要两页(使用不同的登录)

  • 登录页面
  • 如果用户登录,只能访问一个正常的页面。

用户拥有此cookie后,您可以关闭浏览器,再次打开浏览器,然后直接访问受保护的页面(用户不应该被重定向或转到登录页面)

但是,您的日志记录显示,您尝试通过登录页面登录用户,另一方面,您告诉您要使用记住我的记号。 - 对于使用Remember Me Cookie,您不能请求登录页面,而是请求正常页面。

的RememberMeAuthenticationFilter会(当它尝试登录)调试级别这个消息打印:

  • “SecurityContextHolder的填充记得,我的令牌:...”(成功)
  • “ SecurityContextHolder没有填充记忆-Me标记,因为AuthenticationManager被拒绝认证由RememberMeServices返回......“(失败)
+0

这是我想写的确切答案。做得好! – OhadR

+0

@Ralph谢谢。我误解了功能。现在它解释了一切。 – Jatin