Spring Session - Spring Boot
|
The completed guide can be found in the boot sample application. |
Updating
Dependencies
Before you use Spring Session, you must ensure to update your dependencies. We assume you are working with a working Spring Boot web application. If you are using Maven, ensure to add the following dependencies:
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
</dependencies>
Spring
Configuration
After adding the required dependencies, we can create our Spring configuration. The Spring configuration is responsible for creating a Servlet Filter that replaces the HttpSession implementation
with an implementation backed by Spring Session. Add the following Spring Configuration:
@EnableRedisHttpSession
public class HttpSessionConfig { }
The @EnableRedisHttpSession annotation createsa Spring Bean with the name of springSessionRepositoryFilterthatimplements Filter. The filter is what is in charge of replacing the HttpSession implementationto be backed by Spring Session. In this instance Spring Session is backed by Redis. |
Configuring
the Redis Connection
Spring Boot automatically creates a RedisConnectionFactory that
connects Spring Session to a Redis Server on localhost on port 6379 (default port). In a production environment you need to ensure to update your configuration to point to your Redis server. For example, you can include the following in your application.properties
spring.redis.host=localhost
spring.redis.password=secret
spring.redis.port=6379
For more information, refer to Connecting
to Redis portion of the Spring Boot documentation.
Servlet
Container Initialization
Our Spring Configuration created
a Spring Bean named springSessionRepositoryFilter that
implements Filter. The springSessionRepositoryFilter bean
is responsible for replacing the HttpSession with
a custom implementation that is backed by Spring Session.
In order for our Filter to do its magic,
Spring needs to load our Config class. Last
we need to ensure that our Servlet Container (i.e. Tomcat) uses our springSessionRepositoryFilter for
every request. Fortunately, Spring Boot takes care of both of these steps for us.
boot
Sample Application
The boot Sample Application demonstrates how to use Spring Session to transparently leverage Redis to back a web application’s HttpSession when
using Spring Boot.
Running
the boot Sample Application
You can run the sample by obtaining the source code and invoking
the following command:
|
For the sample to work, you must install Redis 2.8+ on localhost and run it with the default port (6379). Alternatively, |
$ ./gradlew :samples:boot:bootRun
You should now be able to access the application at http://localhost:8080/
Exploring
the security Sample Application
Try using the application. Enter the following to log in:
Username user
Password password
Now click the Login button. You should now see a message indicating your are logged in with the user entered previously. The user’s information is stored in Redis rather than Tomcat’s HttpSession implementation.
How
does it work?
Instead of using Tomcat’s HttpSession, we
are actually persisting the values in Redis. Spring Session replaces theHttpSession with
an implementation that is backed by Redis. When Spring Security’sSecurityContextPersistenceFilter saves
the SecurityContext to the HttpSession it
is then persisted into Redis.
When a new HttpSession is created, Spring
Session creates a cookie named SESSION in your browser that contains the id of your session. Go ahead and view the cookies (click for help with Chrome or Firefox).
If you like, you can easily remove the session using redis-cli. For example, on a Linux based system you can type:
$ redis-cli keys '*' | xargs redis-cli del
| The Redis documentation has instructions for installing redis-cli. |
Alternatively, you can also delete the explicit key. Enter the following into your terminal ensuring to replace 7e8383a4-082c-4ffe-a4bc-c40fd3363c5e with
the value of your SESSION cookie:
$ redis-cli del spring:session:sessions:7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
Now visit the application at http://localhost:8080/ and observe that we are no longer authenticated.
Spring Session - Spring Boot的更多相关文章
- 别再让你的微服务裸奔了,基于 Spring Session & Spring Security 微服务权限控制
微服务架构 网关:路由用户请求到指定服务,转发前端 Cookie 中包含的 Session 信息: 用户服务:用户登录认证(Authentication),用户授权(Authority),用户管理(R ...
- 使用Spring Session实现Spring Boot水平扩展
小编说:本文使用Spring Session实现了Spring Boot水平扩展,每个Spring Boot应用与其他水平扩展的Spring Boot一样,都能处理用户请求.如果宕机,Nginx会将请 ...
- SpringBoot2 使用Spring Session集群
有几种办法: 1.扩展指定server利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略.缺点:耦合Tomcat/Jetty等Serv ...
- 通过Spring Session实现新一代的Session管理
长期以来,session管理就是企业级Java中的一部分,以致于我们潜意识就认为它是已经解决的问题,在最近的记忆中,我们没有看到这个领域有很大的革新. 但是,现代的趋势是微服务以及可水平扩展的原生云应 ...
- 转:通过Spring Session实现新一代的Session管理
长期以来,session管理就是企业级Java中的一部分,以致于我们潜意识就认为它是已经解决的问题,在最近的记忆中,我们没有看到这个领域有很大的革新. 但是,现代的趋势是微服务以及可水平扩展的原生云应 ...
- 通过 Spring Session 实现新一代的 Session 管理
长期以来,session 管理就是企业级 Java 中的一部分,以致于我们潜意识就认为它是已经解决的问题,在最近的记忆中,我们没有看到这个领域有很大的革新. 但是,现代的趋势是微服务以及可水平扩展的原 ...
- 基于Spring Boot/Spring Session/Redis的分布式Session共享解决方案
分布式Web网站一般都会碰到集群session共享问题,之前也做过一些Spring3的项目,当时解决这个问题做过两种方案,一是利用nginx,session交给nginx控制,但是这个需要额外工作较多 ...
- Spring boot配合Spring session(redis)遇到的错误
背景:本MUEAS项目,一开始的时候,是没有引入redis的,考虑到后期性能的问题而引入.之前没有引用redis的时候,用户登录是正常的.但是,在加入redis支持后,登录就出错!错误如下: . __ ...
- Spring boot集成spring session实现session共享
最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么se ...
随机推荐
- 如何快速方便的输出向量vector容器中不重复的内容
在vector容器中,存入的内容难免会出现重复,那么如何快速输出或提前非重复的那些数据呢,即重复的数据只输出一次,直观的方法是每次输出都要通过循环比较是否已经输出过,这种方法还是比较费时的,可以利用u ...
- LoaderManager使用具体解释(三)---实现Loaders
这篇文字将介绍Loader<D>类,而且介绍自己定义Loader的实现.这是本系列的第三篇文章. 一:Loaders之前世界 二:了解LoaderManager 三:实现Loaders 四 ...
- Codeforces Round #246 (Div. 2)
题目链接:Codeforces Round #246 (Div. 2) A:直接找满足的人数,然后整除3就是答案 B:开一个vis数组记录每一个衣服的主场和客场出现次数.然后输出的时候主场数量加上反复 ...
- layout_weight属性详解
看到上面这段代码,大家肯定认为三个TextView的宽度是1:2:2,但实际上是如图1这样的,宽度之比确实是1:2:2,但为什么第一个和后两个是齐平的呢?下面我给大家画一条线,可以看到虽然控件是没有对 ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- Java -- WeakHashMap
一.引言 Java中的引用类型由四种情况,强引用.软引用.弱引用.虚引用.关于这些的介绍可以参见鄙人另外一篇博文. ...
- UltraEdit for mac 3.2.0.10免费破解版下载!!
http://www.mactech.cn/a/108.html UltraEdit for mac 3.2.0.10破解版下载地址 看很多朋友不知道算号器的使用方法,分享如下: 1. 解压Ultra ...
- 互联网创业十问?good(快速迭代、把握核心用户应对抄袭,不需要把商业模式考虑完备,4种失败的信号,失败者没资格说趁着年轻...)
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:曹政链接:https://www.zhihu.com/question/20264499/answer/28168079来源: ...
- MYSQL设计优化
本文将从各方面介绍优化mysql设计的一些方式. 1.优化sql语句 (1)定位须要优化的sql语句 1)show status统计SQL语句频率 对Myisam和Innodb存储引擎都计数的參数: ...
- 同步内核缓冲区 sync、fsync和fdatasync函数
同步内核缓冲区 1.缓冲区简单介绍 人生三大错觉之中的一个:在调用函数write()时,我们觉得该函数一旦返回,数据便已经写到了文件里.可是这样的概念仅仅是宏观上的.实际上.操作系统实现某些文件I/O ...