java - Remember Me 如何在 Spring Security 中工作?

标签 java tomcat spring-security remember-me

我很好奇 Remember Me 是如何工作的以及它在 Spring Security 中如何工作?

我知道服务器会向客户端发送长期有效的 cookie。然后客户端发回 cookie,服务器可以识别客户端,因为服务器上有类似 hash-map 的东西与 cookie --> session 的关系。

我不明白服务器[服务器端应用程序]在服务器[Tomcat]重启后如何通过cookie识别客户端。

在服务器关闭之前,Spring Security 如何以及在哪里保存 cookie-session 映射?它是否特定于服务器(即 Tomcat、Jetty 等发生了一些不同的事情)?

附言另一个与 Spring Security 和重新部署相关的问题:即使我没有勾选 RememberMe 并登录,重新部署大约 3 分钟后我仍然被识别出来。它可以修复吗?

最佳答案

The Spring Security docs discuss这实际上是如何工作的。

This approach uses hashing to achieve a useful remember-me strategy. In essence a cookie is sent to the browser upon successful interactive authentication, with the cookie being composed as follows:

base64(username + ":" + expirationTime + ":" + md5Hex(username + ":" + expirationTime + ":" password + ":" + key))

...

As such the remember-me token is valid only for the period specified, and provided that the username, password and key does not change. Notably, this has a potential security issue in that a captured remember-me token will be usable from any user agent until such time as the token expires. This is the same issue as with digest authentication.

基本上,cookie 包含用户名、密码、过期时间和一个 key (您指定),所有这些都是 hashed一起。当您的浏览器将此 cookie 的内容发送到服务器时,Spring Security:

  1. 从后端检索给定用户名的密码
  2. 从数据库中计算用户名/密码等的md5Hex()并将其与cookie中的值进行比较
  3. 如果匹配 - 您已登录!如果不匹配,则说明您提供了伪造的 cookie,或者其中一个用户名/密码/ key 已更改。

这里的基本假设是散列函数 - 上面的 md5Hex() 部分 - 提供了一种方法来轻松地在一个方向上对一些数据片段进行编码,但要逆转(到从 md5Hex 文本中恢复密码。

关于java - Remember Me 如何在 Spring Security 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3142722/

相关文章:

java - 方法级别 Spring Security 的重要性

Java 方法和返回语句

sql-server - 启动 tomcat 服务器时出现 404,在 java 库路径上找不到 Tomcat Native Library

Tomcat 6 记录器不接收 webapp 日志数据

Tomcat:如何在运行时动态设置 keystoreFile 和 keystorePass?

grails - Grails Spring Websocket插件和Spring安全性

java - 需要在java中的数据结构中找出 "key"

java - 如何在 Tomcat 8 中设置 Java 运行时参数

java - 评论树,回复回复即可显示

java - 使用 Spring OAuth2 资源服务器和对称 key 时如何避免 KeyLengthException