Spring Security学习笔记(三)
之前提到过认证后怎么存放用户信息,令牌token是一种方式,session是另一种方式,这里介绍使用spring session data redis存储httpSession。

添加了以上依赖后,我们可以创建Spring Boot 配置。Spring Boot 已经做好了大部分的配置工作,我们只需要做如下配置:
spring.session.store-type=redis # Session store type.
Spring Boot 会自动添加一个@EnableRedisHttpSessionannotation,从而创建了一个springSessionRepositoryFilter。该filter做的事就是将HttpSession转为Spring Session。这里Spring Session 持久化到redis中。
Spring Boot会自动创建一个RedisConnnectionFactory将Spring Session 连接到本地端口6379的Redis Server。在产品环境,你需要确保连接到产品环境的redis server。那么,可以做如下配置:
spring.redis.host=localhost # Redis server host.
spring.redis.password= # Login password of the redis server.
spring.redis.port=6379 # Redis server port.
更多的配置可以参考:https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/#boot-features-session
我们没有使用tomcat的HttpSession,而是将session持久化到redis。当Spring Security的SecurityContextPersistenceFilter将SecurityContext保存到HttpSession时,spring session将该值保存到Redis。
例如你请求login api,登陆成功之后,可以使用redis-cli查看redis中保存到session值:
登陆前

登陆后

当一个新的HttpSession创建了,Spring Session 创建一个名为SESSION的cookie存在浏览器,该值是session_id。
你可以移除session,然后再请求权限保护的api,会返回403。
del spring:session:sessions:7e75811b-8711-4202-ba85-1a0a8142362a
代码repo: https://github.com/Rying/twitter-clone.git
branch:

Spring Security学习笔记(三)的更多相关文章
- SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证
整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...
- SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能
在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...
- Spring Security学习笔记
Spring Web Security是Java web开发领域的一个认证(Authentication)/授权(Authorisation)框架,基于Servlet技术,更确切的说是基于Servle ...
- SpringBoot + Spring Security 学习笔记(二)安全认证流程源码详解
用户认证流程 UsernamePasswordAuthenticationFilter 我们直接来看UsernamePasswordAuthenticationFilter类, public clas ...
- Spring Security学习笔记一
一.使用Spring Security 1.在pom 文件中添加Spring Security的依赖. <dependency> <groupId>org.springfram ...
- SpringBoot + Spring Security 学习笔记(一)自定义基本使用及个性化登录配置
官方文档参考,5.1.2 中文参考文档,4.1 中文参考文档,4.1 官方文档中文翻译与源码解读 SpringSecurity 核心功能: 认证(你是谁) 授权(你能干什么) 攻击防护(防止伪造身份) ...
- Spring security学习笔记(二)
对比两种承载认证信息的方式: session vs token token验证方案: session验证方案: session即会话是将用户信息保存在服务端,根据请求携带的session_id,从服务 ...
- Spring Security学习笔记(一)
认证和权限控制 AuthenticationManager是认证的主要接口,它只有一个authenticate方法,可以做3件事情. 返回一个认证信息(Authentication),表示认证成功 抛 ...
- Spring Security 学习笔记-securityContext过滤器过滤链学习
web.xml配置委托代理filter,filter-name默认与filter bean的名字保持一致. <filter> <filter-name>springSecuri ...
随机推荐
- dom操作排他思想
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- GTX650Ti,GT610安装黑苹果之经验与步骤
安装这两个显卡的黑苹果都是10.9以上的版本,一个是10.9.2,一个是10.9.4,最后都完美.主板一个是Z77,一个是H61. 1. 开始安装完以后,显卡不工作,能够安全模式进去. 2. 删除Ap ...
- c# BackGroundWorker 多线程操作的小例子 (转)
在我们的程序中,经常会有一些耗时较长的运算,为了保证用户体验,不引起界面不响应,我们一般会采用多线程操作,让耗时操作在后台完成,完成后再进行处理或给出提示,在运行中,也会时时去刷新界面上的进度条等显示 ...
- pt-deadlock-logger使用
死锁监控pt-deadlock-logger 首先我们要创建一个表用来保存死锁的信息: CREATE TABLE deadlocks ( server ) NOT NULL, ts timestamp ...
- ubuntu安装最新版node和npm
1.先在系统上安装好nodejs和npm sudo apt-get install nodejs-legacy sudo apt-get install npm 2.升级n ...
- AJAX原生JavaScript写法
GET方式 //创建XMLHttpRequest对象,为考虑兼容性问题,老版本的 Internet Explorer (IE5 和 IE6)使用 ActiveX 对象 var ajax = windo ...
- C++ int与string的相互转换(含源码实现)
一.int转换成string Ⅰ.to_string函数 c++11标准增加了全局函数std::to_string: string to_string (int val); string to_str ...
- linux中无法使用sudo的方法
xxx is not in the sudoers file.This incident will be reported.的解决方法 1.切换到root用户下. 2.添加sudo文件的写权限,命令是 ...
- ZT 创建类模式总结篇
创建类模式总结篇 分类: 设计模式 2012-03-26 09:03 7320人阅读 评论(11) 收藏 举报 编程优化设计模式任务 创建类模式主要关注对象的创建过程,将对象的创建过程进行封装,使客户 ...
- SOJ4478 Easy Problem II(模拟、栈)
Time Limit: 3000 MS Memory Limit: 131072 K Description 在数据结构中 我们学习过 栈 这种数据结构 通过栈 我们可以将1,2,3,...,n转化成 ...