之前提到过认证后怎么存放用户信息,令牌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学习笔记(三)的更多相关文章

  1. SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证

    整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...

  2. SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能

    在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...

  3. Spring Security学习笔记

    Spring Web Security是Java web开发领域的一个认证(Authentication)/授权(Authorisation)框架,基于Servlet技术,更确切的说是基于Servle ...

  4. SpringBoot + Spring Security 学习笔记(二)安全认证流程源码详解

    用户认证流程 UsernamePasswordAuthenticationFilter 我们直接来看UsernamePasswordAuthenticationFilter类, public clas ...

  5. Spring Security学习笔记一

    一.使用Spring Security 1.在pom 文件中添加Spring Security的依赖. <dependency> <groupId>org.springfram ...

  6. SpringBoot + Spring Security 学习笔记(一)自定义基本使用及个性化登录配置

    官方文档参考,5.1.2 中文参考文档,4.1 中文参考文档,4.1 官方文档中文翻译与源码解读 SpringSecurity 核心功能: 认证(你是谁) 授权(你能干什么) 攻击防护(防止伪造身份) ...

  7. Spring security学习笔记(二)

    对比两种承载认证信息的方式: session vs token token验证方案: session验证方案: session即会话是将用户信息保存在服务端,根据请求携带的session_id,从服务 ...

  8. Spring Security学习笔记(一)

    认证和权限控制 AuthenticationManager是认证的主要接口,它只有一个authenticate方法,可以做3件事情. 返回一个认证信息(Authentication),表示认证成功 抛 ...

  9. Spring Security 学习笔记-securityContext过滤器过滤链学习

    web.xml配置委托代理filter,filter-name默认与filter bean的名字保持一致. <filter> <filter-name>springSecuri ...

随机推荐

  1. python mqtt client publish操作

    使用Python库paho.mqtt.client 模拟mqtt client 连接broker,publish topic. #-*-coding:utf-8-*- import paho.mqtt ...

  2. nginx https 连接加密

    ##HTTPS server##server {listen 443;server_name www.shabi.com;ssl on;index index.php index.html index ...

  3. python shopping incomplete code

    #shopping code#shopping.py#导入登录模块import login# shop car beginningsalary = input("请输入工资:\t" ...

  4. C#启动服务

    启动服务的方法有很多种,简单的cmd下dos命名,手动启动,还有C#代码启动. 我们要实现的功能: 判断是否安装 是否启动 启动服务 关闭服务 我封装了有关服务的代码,如下: using System ...

  5. Data Flow ->> Excel Connection遇到错误:[Excel Source [16]] Error: SSIS Error Code DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER.....

    在SSIS下做Excel导入数据的时候遇到下面的错误 [Excel Source [16]] Error: SSIS Error Code DTS_E_CANNOTACQUIRECONNECTIONF ...

  6. 【Leetcode】【Hard】Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  7. Python学习---内置函数的学习

    内置函数 [Py3.5官方文档]https://docs.python.org/3.5/library/functions.html#abs Built-in Functions abs() dict ...

  8. IntelliJ IDEA下"Cannot resolve symbol 'log'"的解决方法

    转自:https://my.oschina.net/greatqing/blog/703989 最近接手了一个Maven项目,IDE使用的是IntelliJ IDEA,导入后可以编译运行.但是输出日志 ...

  9. mysql多实例mysqld_multi方式

    mysql多实例应用,亲测直接执行脚本可使用,可快速部署多实例环境 #!/bin/bash set -e #定义mysql_multi多实例数据的配置,如需增加,在后面函数对应地方需要增加 mydir ...

  10. Kafka与MQ的区别

    作为消息队列来说,企业中选择mq的还是多数,因为像Rabbit,Rocket等mq中间件都属于很成熟的产品,性能一般但可靠性较强, 而kafka原本设计的初衷是日志统计分析,现在基于大数据的背景下也可 ...