1。概述

本教程将展示如何使用 Spring Security 在 Web 应用程序中启用和配置 Remember Me。之前已经讨论过设置安全和简单表单登录的 MVC 应用程序。

该机制将能够跨多个会话识别用户——首先要了解的是,Remember Me 仅在会话超时后才会启动。默认情况下,用户在 30 分钟不活跃后会超时,但在 web.xml 中可以配置超时时间。

注意:本教程重点介绍基于标准 cookie 的方法。对于持久化方法,请查看 Spring Security – Persistent Remember Me 指南。

2。安全配置

让我们看看如何使用 Java 设置安全配置:

@Configuration
@EnableWebSecurity
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {@Bean("authenticationManager")@Overridepublic AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();}@Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("user1").password("{noop}user1Pass").roles("USER").and().withUser("admin1").password("{noop}admin1Pass").roles("ADMIN");}@Overrideprotected void configure(final HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/anonymous*").anonymous().antMatchers("/login*").permitAll().anyRequest().authenticated().and().formLogin().loginPage("/login.html").loginProcessingUrl("/login").failureUrl("/login.html?error=true").and().logout().deleteCookies("JSESSIONID").and().rememberMe().key("uniqueAndSecret");}
}

如您所见,使用 rememberMe() 的基本配置非常简单,同时通过附加选项保证可扩展性。 key 在这里很重要——它是整个应用程序的私有秘钥,将在生成令牌的内容时使用。

此外,可以使用 tokenValiditySeconds() 将 ** 令牌有效时间从默认的 2 周配置为** - 例如 - 一天:

rememberMe().key("uniqueAndSecret").tokenValiditySeconds(86400)

我们还可以看看等价的 XML 配置:

<http use-expressions="true"><intercept-url pattern="/anonymous*" access="isAnonymous()" /><intercept-url pattern="/login*" access="permitAll" /><intercept-url pattern="/**" access="isAuthenticated()" /><form-login login-page='/login.html'authentication-failure-url="/login.html?error=true" /><logout delete-cookies="JSESSIONID" /><remember-me key="uniqueAndSecret"/>
</http><authentication-manager id="authenticationManager"><authentication-provider><user-service><user name="user1" password="{noop}user1Pass" authorities="ROLE_USER" /><user name="admin1" password="{noop}admin1Pass" authorities="ROLE_ADMIN" /></user-service></authentication-provider>
</authentication-manager>

3。登录表格

登录表单类似于我们用于表单登录的那个:

<html>
<head></head><body><h1>Login</h1><form name='f' action="login" method='POST'><table><tr><td>User:</td><td><input type='text' name='username' value=''></td></tr><tr><td>Password:</td><td><input type='password' name='password' /></td></tr><tr><td>Remember Me:</td><td><input type="checkbox" name="remember-me" /></td></tr><tr><td><input name="submit" type="submit" value="submit" /></td></tr></table></form></body>
</html>

注意新添加的 checkbox 输入会映射到 remember-me。这个添加的输入足以在Remember Me激活的情况下登录。

此默认路径也可以更改为:

.rememberMe().rememberMeParameter("remember-me-new")

4。Cookie

该机制将在用户登录时创建一个额外的 cookie——“remember-me” cookie。

Remember Me cookie 包含以下数据:

  • 用户名 – 标识登录的主体
  • expirationTime – 使 cookie 过期;默认为 2 周
  • MD5 hash – 前 2 个值的 – usernameexpirationTime ,加上 password 和预定义的 key

这里首先要注意的是 usernamepassword 都是 cookie 的一部分——这意味着,如果其中任何一个被更改,cookie 将不再有效。此外,可以从 cookie 中读取 username

此外,如果 Remember Me 的 cookie 被捕获,此机制可能会受到攻击。 cookie 将是有效且可用的,直到它过期或更改凭据。

5。实际使用

要查看 Remember Me 机制的工作情况,您需要:

  • 登录时记得我活跃
  • 等待会话过期(或删除浏览器中的 JSESSIONID cookie)
  • 刷新页面

如果 Remember Me 已经失效,cookie 过期后用户应该被 redirected back to the login page 。登录以后用户会在在新令牌/cookie 的帮助下保持登录状态

6。结论

本教程展示了如何在安全配置中设置和配置 Remember Me,并简要描述了记录在 cookie 的数据类型。

具体实现可以在示例 Github 项目 – 这是一个基于 Eclipse 的项目,所以它应该很容易导入和运行。

当项目在本地运行时,可以在 [localhost] 上访问 login.html(http://localhost:8080/spring-security-mvc-custom/login.html “Access the project on localhost”)。

Spring Security 配置 Remember Me相关推荐

  1. Spring Security配置错误

    我最近看到Mike Wienser的SpringOne2GX谈论了Application Security Pitfalls . 如果您在Servlet容器上使用Spring的堆栈,这将非常有用,值得 ...

  2. 从零开始java安全权限框架篇(一):spring security配置登录登出的配置

    目录 一:安全权限框架的选取 二:功能 三:登录登出 四:代码注释 1.将登陆交由Spring security完成 2.前台明文密码加密,与数据库比对 3.关键配置 4.自定义用户异常 5.ajax ...

  3. Spring Security配置全局 AuthenticationManager

    Topical Guide | Spring Security Architecture 默认的全局 AuthenticationManager @Configuration public class ...

  4. Spring Boot——配置Spring Security配置类DEMO

    源代码 package club.zstuca.myzstu.filter;import club.zstuca.myzstu.entity.Resource; import club.zstuca. ...

  5. Spring Security 配置白名单访问后,仍然报错403

    按照框架上配置, .antMatchers("/test/abc").anonymous() 增加 自己路径白名单的配置,结果访问仍然出现403 forbidden 原因在于 没有 ...

  6. spring security配置详解

    1.<intercept-url pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY"/> ...

  7. 史上最简单的Spring Security教程(二十八):CA登录与默认用户名密码登录共存详细实现及配置

    ​在前面的文章中,我们自定义了一些CA登录相关的类,如 CertificateAuthorityAuthenticationToken.CertificateAuthorityAuthenticati ...

  8. 基于 Spring Security 搭建用户权限系统(二) - 自定义配置

    说明 本文的目的是如何基于 Spring Security 去扩展实现一个基本的用户权限模块, 内容会覆盖到 Spring Security 常用的配置. 文中涉及到的业务代码是不完善的, 甚至会存在 ...

  9. 【Spring Security】WebSecurityConfigurerAdapter被deprecated怎么办?官方推荐新的Security配置风格总结

    h 本期目录 背景 一. 前言 二. 配置HttpSecurity 三. 配置WebSecurity 四. 配置LDAP认证 五. 配置JDBC认证 六. In-Memory Authenticati ...

最新文章

  1. 26.C++- 泛型编程之类模板(详解)
  2. VTK:图像方向用法实战
  3. 越过0到1的坎,卖好车开启1到10的路有难题?
  4. 根据大小分割大文本_场景文本检测—CTPN算法介绍
  5. python系统提供构造函数传入参数_python类变量与构造函数的使用
  6. android动画之布局动画,Android动画--布局动画 LayoutAnimation
  7. word中表格占满一页,在后面出现一个空白页 删除方法
  8. 用账号连无线网怎么连接网络连接服务器,路由器怎么共用一个宽带账号
  9. 计算机网络学习笔记(14. OSI参考模型②)
  10. 关于Error.captureStackTrace
  11. android支付宝余额怎么做,android实现类似于支付宝余额快速闪动的效果
  12. fprintf fscanf等函数的用法
  13. 上班时间应该包含交通时间
  14. 管理感悟:不要过度自我合理化
  15. 暑期读书月开幕式暨天一寻宝活动
  16. 要如何给PDF解密?这几个操作简单的方法可以试试
  17. 基于RFM模型的Kmeans聚类算法实现
  18. 一周企业财报 | 阿迪达斯、盖璞、Natura、舍弗勒、百世集团等11家企业发布业绩...
  19. jMonkeyEngine译文 FlagRush1——通过SimpleGame创建你的第一个应用程序
  20. Android应用CPU实时监控工具-全机型适用

热门文章

  1. 从技术 Leader 的招聘需求看,如何转岗为当前紧缺的大数据相关人才?
  2. 什么是API?(详细解说)
  3. CF329B Biridian Forest
  4. 基于chatgpt开发QQ机器人
  5. 在微软工作有多舒服?不加班,最高20天全薪年假,下班有时间玩狼人杀
  6. failed to connect to ‘192.168.31.157:5555‘: Connection refused
  7. Yolov3-v5正负样本匹配机制
  8. 计算机专业铁路局面试题目,铁路局招聘面试
  9. YOLOv5~目标检测模型精确度
  10. VS2019读取文件中文乱码问题解决