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 ...
随机推荐
- Tomcat下JDBC连接样例
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- notepad ++ 编辑 powershell profile 文件时的诡异问题
使用notepad 编辑 C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 时,记事本打开和用notepad++打开显示的内容居然不一样. ...
- 2维FFT算法实现——基于GPU的基2快速二维傅里叶变换
上篇讲述了一维FFT的GPU实现(FFT算法实现——基于GPU的基2快速傅里叶变换),后来我又由于需要做了一下二维FFT,大概思路如下. 首先看的肯定是公式: 如上面公式所描述的,2维FFT只需要拆分 ...
- WSDM 2014推荐系统论文
Xiao Yu, Hao Ma, Paul Hsu, Jiawei Han On Building Entity Recommender Systems Using User Click Log an ...
- spring的声明式的事物管理和编程事务管理的区别
一.Spring对编程式事务的支持 Spring中的事务分为物理事务和逻辑事务: 物理事务:就是底层数据库提供的事务支持,如JDBC或JTA提供的事务: 逻辑事务:是Spring管理的事务,不同于物理 ...
- linux解压eclipse启动时无法找到jre环境的解决办法
使用软链接的方法: 1.打开终端进入到eclipse安装主目录下:mkdir jre 2.cd jre 3.ln -s /home/zhoushuo/app/jdk1.8.0_102/bin bin
- UVa 1626 - Brackets sequence(区间DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- STL - next_permutation 全排列函数
学习: http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html http://blog.csdn.net/ac_gibson/article/deta ...
- 2019.1.3 Mac安装免费版StarUml3.0.2 &&&Xmind思维导图 &&Google浏览器***版
下载StarUml http://staruml.io/ 好像需要FQ 附赠一个 链接:https://pan.baidu.com/s/1_pa9LwopowhOTum5g89zZQ 密码:fxtc ...
- 根据身份证获取地址(mysql)
e Encoding : Date: :: */ ; -- ---------------------------- -- Table structure for `s_area_code` -- - ...