Spring Session - Spring Boot
2015-09-22 10:34
465 查看
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:pom.xml
<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 HttpSessionimplementation
with an implementation backed by Spring Session. Add the following Spring Configuration:
@EnableRedisHttpSession public class HttpSessionConfig { }
The @EnableRedisHttpSessionannotation creates a Spring Bean with the name of springSessionRepositoryFilterthat implements Filter. The filter is what is in charge of replacing the HttpSessionimplementation to be backed by Spring Session. In this instance Spring Session is backed by Redis. |
Configuring
the Redis Connection
Spring Boot automatically creates a RedisConnectionFactorythat
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
src/main/resources/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 createda Spring Bean named
springSessionRepositoryFilterthat
implements
Filter. The
springSessionRepositoryFilterbean
is responsible for replacing the
HttpSessionwith
a custom implementation that is backed by Spring Session.
In order for our
Filterto do its magic,
Spring needs to load our
Configclass. Last
we need to ensure that our Servlet Container (i.e. Tomcat) uses our
springSessionRepositoryFilterfor
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 HttpSessionwhen
using Spring Boot.
Running
the boot Sample Application
You can run the sample by obtaining the source code and invokingthe following command:
For the sample to work, you must install Redis 2.8+ on localhost and run it with the default port (6379). Alternatively, you can update the JedisConnectionFactoryto point to a Redis server. |
$ ./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
HttpSessionimplementation.
How
does it work?
Instead of using Tomcat’s HttpSession, we
are actually persisting the values in Redis. Spring Session replaces the
HttpSessionwith
an implementation that is backed by Redis. When Spring Security’s
SecurityContextPersistenceFiltersaves
the
SecurityContextto the
HttpSessionit
is then persisted into Redis.
When a new
HttpSessionis 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. |
7e8383a4-082c-4ffe-a4bc-c40fd3363c5ewith
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
- Java中double和float的区别
- Spring MVC之hello world
- java自学日记7
- 如何有效地记录 Java SQL 日志?
- java自学日记6
- eclipse重新编译源代码(二)
- java自学日记5
- eclipse重新编译源代码(一)
- JAVA中利用File类遍历文件时空指针报错
- java自学日记4
- java自学日记3
- Java中的instanceof关键字
- MyEclipse开发习惯
- eclipse and systemtap
- 15_09_22 Java 中写方法注意事项
- java中equals 和 == 的区别
- java解决跨域问题
- Spring拦截器
- java 学习银行调度学习笔记