spring security 实现免登陆功能大体也是基于COOKIE来实现的。

主要配置信息:

authentication-success-handler-ref="authenticationSuccessHandler"

services-alias="rememberMeServices" />

1.首先登陆表单要Post URL: /j_spring_security_check 同时_spring_security_remember_me要等于yes,这时登陆后会记录cookie到数据库中;

/j_spring_security_check?_spring_security_remember_me=yes

代码逻辑:

UsernamePasswordAuthenticationFilter 登陆验证过滤器拦截/j_spring_security_check同时调用AbstractRememberMeServices 接口实现

this.rememberMeServices.loginSuccess(request, response, authResult);

2.当会话失效时,这个时候RememberMeAuthenticationFilter 过滤器会调用this.rememberMeServices.autoLogin(request, response);自动登陆;

同时successHandler.onAuthenticationSuccess(request, response, rememberMeAuth); 可以进行一些会话信息加载,这个地方需要根据项目的需要进行改造。

AbstractRememberMeServices

public final Authentication autoLogin(HttpServletRequest request, HttpServletResponse response) {

1.根据spring security 的cookiename获取spring security 保存的cookie

String rememberMeCookie = extractRememberMeCookie(request);

。。。。。

2.解析rememberMeCookie的信息

UserDetails user = null;

String[] cookieTokens = decodeCookie(rememberMeCookie);

3.     获取cookie中信息,并生成登陆的session

user = processAutoLoginCookie(cookieTokens, request, response);

userDetailsChecker.check(user);

logger.debug("Remember-me cookie accepted");

return createSuccessfulAuthentication(request, user);cancelCookie(request, response);

return null;

}

processAutoLoginCookie方法由子类PersistentTokenBasedRememberMeServices  ,TokenBasedRememberMeServices来进行实现;

PersistentTokenBasedRememberMeServices 在登陆时保存登陆时的cookie备份,在处理processAutoLoginCookie时会首先比对cookie是否属于伪造的,

不是伪造的才可以获取登陆信息,进行登陆;这一点非常重要!!!

PersistentTokenBasedRememberMeServices 会实例化加密后的cookie信息到PersistentTokenRepository 接口的实现中,

private PersistentTokenRepository tokenRepository = new InMemoryTokenRepositoryImpl();

InMemoryTokenRepositoryImpl  的存储方式:Map seriesTokens = new HashMap();

JdbcTokenRepositoryImpl 的存储方式数据库表:推荐使用JdbcTokenRepositoryImpl 方式,这样集群环境下也可以实现cookie的信息的机器备份;

public static final String CREATE_TABLE_SQL =

"create table persistent_logins (username varchar(64) not null, series varchar(64) primary key, " +

"token varchar(64) not null, last_used timestamp not null)";

官方配置reference参考:

Attributes

authentication-success-handler-refSets theauthenticationSuccessHandlerproperty on theRememberMeAuthenticationFilterif custom navigation is required. The value should be the name of aAuthenticationSuccessHandlerbean in the application context.

data-source-refA reference to aDataSourcebean. If this is set,PersistentTokenBasedRememberMeServiceswill be used and configured with aJdbcTokenRepositoryImplinstance.

remember-me-parameterThe name of the request parameter which toggles remember-me authentication. Defaults to "_spring_security_remember_me". Maps to the "parameter" property ofAbstractRememberMeServices.

keyMaps to the "key" property ofAbstractRememberMeServices. Should be set to a unique value to ensure that remember-me cookies are only valid within the one application[26]. If this is not set a secure random value will be generated. Since generating secure random values can take a while, setting this value explicitly can help improve startup times when using the remember me functionality.

services-aliasExports the internally definedRememberMeServicesas a bean alias, allowing it to be used by other beans in the application context.

services-refAllows complete control of theRememberMeServicesimplementation that will be used by the filter. The value should be theidof a bean in the application context which implements this interface. Should also implementLogoutHandlerif a logout filter is in use.

token-repository-refConfigures aPersistentTokenBasedRememberMeServicesbut allows the use of a customPersistentTokenRepositorybean.

token-validity-secondsMaps to thetokenValiditySecondsproperty ofAbstractRememberMeServices. Specifies the period in seconds for which the remember-me cookie should be valid. By default it will be valid for 14 days.

use-secure-cookieIt is recommended that remember-me cookies are only submitted over HTTPS and thus should be flagged as "secure". By default, a secure cookie will be used if the connection over which the login request is made is secure (as it should be). If you set this property tofalse, secure cookies will not be used. Setting it totruewill always set the secure flag on the cookie. This attribute maps to theuseSecureCookieproperty ofAbstractRememberMeServices.

user-service-refThe remember-me services implementations require access to aUserDetailsService, so there has to be one defined in the application context. If there is only one, it will be selected and used automatically by the namespace configuration. If there are multiple instances, you can specify a beanidexplicitly using this attribute.

security 底层原理_spring security 实现remeber me(免登陆功能)的原理相关推荐

  1. bic计算机原理,CBTC系统区域控制器(ZC)功能及原理探究

    屈耀 摘  要:地面区域控制器(ZC)作为CBTC系统的核心地面设备,剖析其功能.工作原理及外部通信,对我们深化认知,学习和理解有着极为重要意义. 关键词:区域控制器;ZC;CBTC 中图分类号:U2 ...

  2. java security 详解_Spring Security Remember me使用及原理详解

    Remember me功能就是勾选"记住我"后,一次登录,后面在有效期内免登录. 先看具体配置: pom文件: org.springframework.boot spring-bo ...

  3. 基于Spring Security的认证方式_编程理解PasswordEncoder工作原理_Spring Security OAuth2.0认证授权---springcloud工作笔记125

    技术交流QQ群[JAVA,C++,Python,.NET,BigData,AI]:170933152 .然后我们再看一下PasswordEncoder 这个编码器,密码加密器怎么用. 可以看到现在我们 ...

  4. java security 详解_Spring Security入门教程 通俗易懂 超详细 【内含案例】

    Spring Security的简单使用 简介 SSM 整合 Security 是比较麻烦的,虽然Security的功能比 Shiro 强大,相反却没有Shiro的使用量多 SpringBoot出现后 ...

  5. idea查询类_Spring Security入门(三): 基于自定义数据库查询的认证实战

    0 引言 在笔者的上一篇文章中Spring Security入门(二):基于内存的认证一文中有提到过Spring Security实现自定义数据库查询需要你实现UserDetailsService接口 ...

  6. Spring Security实现RememberMe功能以及原理探究

    在大多数网站中,都会实现RememberMe这个功能,方便用户在下一次登录时直接登录,避免再次输入用户名以及密码去登录,下面,主要讲解如何使用Spring Security实现记住我这个功能以及深入源 ...

  7. springsecurity默认用户名密码_Spring Security概述

    简介 Spring Security是一个功能强大.高度可定制的身份验证和访问控制框架.它实际上是保护基于Spring的应用程序的标准. Spring Security是一个专注于为Java应用程序提 ...

  8. 美年_移动端开发_权限控制_Spring Security入门与进阶

    文章目录 第三章权限控制 3.1. 认证和授权概念 3.2. 权限模块数据模型 3.3 RBAC权限模型扩展: [小结] 3.3. Spring Security简介 [讲解] [小结] 3.4. S ...

  9. security工作笔记007---spring security自定义AuthenticationProvider,验证规则

    JAVA技术交流QQ群:170933152 AuthenticationProvider 认证是由 AuthenticationManager 来管理的,但是真正进行认证的是 Authenticati ...

最新文章

  1. STK 卫星覆盖分析笔记
  2. ArcGIS Engine生成等值线(C#)
  3. numpy随机生成01矩阵_NumPy数组基本介绍
  4. Python数据可视化实战应用万字长文从入门到高端(建议收藏)
  5. Unity shader学习之Grab Pass实现玻璃效果
  6. kafka生产者消息分区策略
  7. Linux用户管理详解大结局(下)
  8. PyCharm社区版支持深度学习_在Windows的Ubuntu子系统运行支持CUDA的深度学习代码
  9. ArcGIS 创建格网与图斑数据叠加;频数统计与面积比例计算
  10. iOS 取消多余tableView的横线的写法
  11. java中的sqlist,JAVA连接SQList数据库 | 学步园
  12. 一个Python 爬虫程序
  13. web调用IC卡读卡器开发第七章--NFC标签NDEF数据
  14. 区县级应急综合管理系统功能
  15. 贱人工具箱使用技巧5——批量修改文字
  16. 谈一谈post和get的区别
  17. auto.js B0013 查找父控件子控件进入阅读文章视频学习每日答题2021-10-03
  18. STM32F429第二十八篇之ADC
  19. 清华大学06届 计算机王煜,2006年科研成果一览表
  20. 2019校招宜信JAVA研发面经

热门文章

  1. mysql创建主键索引的关键字使用_mysql字段、主键、索引等的创建与修改命令
  2. java 时间段内月份_java获取某段时间内的月份列表
  3. Please make sure you have the correct access rights and the repository exists.报错问题
  4. IllegalArgumentException:@Body parameters cannot be used with form or multi-part encoding
  5. 剖析大数据平台的数据采集
  6. Yii2系列教程三:Database And Gii
  7. 线段树 + 矩阵 --- ZOJ 3772 Calculate the Function
  8. 关于java中的数组
  9. 崽崽的发烧终于快结束了
  10. 用chkconfig配置linux自启动服务