spring security 实现免登陆功能大体也是基于COOKIE来实现的。
主要配置信息:
<remember-me data-source-ref="dataSource" key="rememberMeCookie"
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<String, PersistentRememberMeToken> seriesTokens = new HashMap<String, PersistentRememberMeToken>();
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参考:
<remember-me> Attributes
authentication-success-handler-ref Sets the authenticationSuccessHandler property on theRememberMeAuthenticationFilter if custom navigation is required. The value should be the name of aAuthenticationSuccessHandler bean in the application context.
data-source-ref A reference to a DataSource bean. If this is set, PersistentTokenBasedRememberMeServices will be used and configured with a JdbcTokenRepositoryImpl instance.
remember-me-parameter The name of the request parameter which toggles remember-me authentication. Defaults to "_spring_security_remember_me". Maps to the "parameter" property of AbstractRememberMeServices.
key Maps to the "key" property of AbstractRememberMeServices. 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-alias Exports the internally defined RememberMeServices as a bean alias, allowing it to be used by other beans in the application context.
services-ref Allows complete control of the RememberMeServices implementation that will be used by the filter. The value should be the id of a bean in the application context which implements this interface. Should also implement LogoutHandler if a logout filter is in use.
token-repository-ref Configures a PersistentTokenBasedRememberMeServices but allows the use of a customPersistentTokenRepository bean.
token-validity-seconds Maps to the tokenValiditySeconds property of AbstractRememberMeServices. 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-cookie It 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 to false, secure cookies will not be used. Setting it to true will always set the secure flag on the cookie. This attribute maps to the useSecureCookie property of AbstractRememberMeServices.
user-service-ref The remember-me services implementations require access to a UserDetailsService, 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 bean id explicitly using this attribute.
分享到:
相关推荐
登录过程中经常使用的“记住我”功能,也就是我们经常会在各种网站登陆时见到的"两周内免登录",“三天内免登录”的...今天小编给大家分享基于Spring Security实现两周内自动登录"记住我"功能,感兴趣的朋友一起看看吧
采用SpringBoot3.1、SpringSecurity6.1、Mybatis-Plus等框架,开发的一套企业级低代码开发平台,使用门槛极低,且采用MIT开源协议,完全免费开源,可免费用于商业项目等场景,采用组件模式,扩展不同的业务功能,...
基于springsecurity+springmvc+spring+hibernate的权限管理系统,实现资源、用户、权限、角色的增删改查,角色-资源管理,用户-角色管理等基础功能,可以作为springmvc+spring+hibernate的增删改查入门项目,也可以...
Spring Security模块配置 模组名称 解释 Spring安全核心 由核心组件组成,以实现身份验证和授权功能 Spring安全网 包含实现Web应用程序安全功能的组件 弹簧安全配置 由组件组成,以支持每个模块提供的组件配置(支持...
maku-boot 是采用SpringBoot3.0、SpringSecurity6.0、Mybatis-Plus等框架,开发的一套SpringBoot低代码开发平台,使用门槛极低,且采用MIT开源协议,完全免费开源,可免费用于“ 商业项目”等场景。采用组件模式,...
此外,系统可能还会采用Spring Security处理用户安全和权限验证,使用Thymeleaf或Freemarker作为模板引擎渲染前端页面,以及MySql或MongoDB作为后端数据库存储用户数据和聊天记录。网络聊天室系统的设计与实现不仅...
java权限后台开发框架,采用spring + srpingsecurity + springMVC + Hibernate + freemarker + jquery 等技术实现,页面及源文件打包发布程序。 完整的功能应用,包括:后台权限、人员机构、参数代码、角色权限、...
Jeepay使用Spring Boot和Ant Design Vue开发, 集成Spring Security实现权限管理功能,是一套非常实用的web开发框架。 资源是开源无BUG,请自行安装 不提供免费指导服务 不提供免费安装服务,如需付费安装请私信我
包括了完整的后台管理功能,如:用户、机构、代码、角色、功能权限,集成了springsecurity3.0来保证安全。 具体可参考上个版本的描述: http://download.csdn.net/detail/pxzsky/6659071 新版本更新内容如下: 1、...
整合WebSocket技术实现实时通信功能。使用Vuex进行状态管理,Vue Router处理页面路由,提升用户体验与界面响应速度。 2.2 后端模块 后端架构采用Spring Boot进行微服务的快速开发,结合SpringMVC进行请求处理和响应...
java权限后台开发框架,采用spring + srpingsecurity + springMVC + Hibernate + freemarker + jquery 等技术实现,页面及源文件打包发布程序。 完整的功能应用,包括:后台权限、人员机构、参数代码、角色权限、...
6. **安全性设计**:后端采用Spring Security进行安全控制,确保了用户认证和授权的安全性,保护交易数据不被未授权访问。 7. **界面友好性**:前端使用JSP构建,提供了适合花店业务的清晰、直观且响应式的用户界面...
应用具有高度独立性,不依赖认证中心,又可用使用认证中心的认证,实现单点登录。 7、基于Java平台开发,采用Spring、MySQL、Tomcat、Apache Kafka、Redis等开源技术,支持微服务,扩展性强。 8、许可证 Apache ...
基于 JWT 实现前后分离,Spring Security 安全控制 前端采用 topjui(基于 easyui 的美化框架 ) 实现了三级动态菜单、页面按钮动态授权功能 实现了用户管理、菜单管理、角色管理 集成 Swagger 接口管理
maku-boot 是采用SpringBoot3.0、SpringSecurity6.0、Mybatis-Plus等框架,开发的一套企业级脚手架系统,使用门槛极低,且采用MIT开源协议,完全免费开源,可免费用于商业项目等场景。 项目特点 采用组件模式,通过...
Spring Security 5.1.4.RELEASE 认证和授权框架 MyBatis 3.4.6 ORM框架 MyBatisGenerator 1.3.3 数据层代码生成 PageHelper 5.1.8 MyBatis物理分页插件 Maven 3.6.1 项目管理工具 Swagger2 2.7.0 交互式API文档 ...
SpringSecurity OAuth 2客户端登录适配 移除Desktop的支持,后续可以开发FormBase的适配器定制 application.properties profiles的优化,不同环境启动更加简单 删除maxkey.properties,配置整合到 application....
百度地图开发java源码 elasticsearch-house 基于Elasticsearch的租房搜索系统 概述 一、系统架构 主要是以MySQL作为基础数据存储,结合ES实现站内搜索引擎。...免注册登录: 阿里短信(基于阿里云通
初步采用Spring Boot,Spring Security,Redis&Jwt。权限认证使用Jwt,支持多终端认证系统。支持加载动态权限菜单,多方式轻松权限控制。高效率开发,使用代码生成器可以一键生成前的代码。提供了单应用版本 ,...
GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...