Spring Boot + Session Management Example Using Redis
In this post we will be implementing Session Management + Spring Boot +Redis where we used Redis as the data store for storing Session Information.
First let us have a look at what is session management and how can it be accomplished.
What is Session Management?
We all know that HTTP is a stateless protocol. All requests and responses are independent. The server cannot distinguish between new visitors and returning visitors. But sometimes we may need to keep track of client's activity across multiple requests. This is achieved using Session Management. It is a mechanism used by the Web container to store session information for a particular user.Session management can be achieved in one of the following ways-
- Cookies
- Hidden form field
- URL Rewriting
- HttpSession
Spring Session consists of the following modules:
- Spring Session Core - provides core Spring Session functionalities and APIs
- Spring Session Data Redis - provides SessionRepository and ReactiveSessionRepository implementation backed by Redis and configuration support
- Spring Session JDBC - provides SessionRepository implementation backed by a relational database and configuration support
- Spring Session Hazelcast - provides SessionRepository implementation backed by Hazelcast and configuration support
In order to achieve writing the session objects to Redis, we dont have to write any code. Spring Boot provides us this functionality out of the box by specifying the following configuration property
spring.session.store-type=redis
Lets Begin-
Getting started with Redis
We will first need to install Redis. For this follow the below steps-- Go to Redis Downloads Page at github and download the Redis zip file.
- Go to the location where the downloaded contents are unzipped
Use the following command to start Redis Serverredis-server.exe --maxheap 1024M
- Open a new Command Line Window and again go to the location where the downloaded contents are unzipped and use the following
command to start the Redis Client which we will use to monitor Redis.
redis-cli.exe
Once started use the monitor command as follows-monitor
Spring Boot + Session Management + Redis
Maven Project will be as follows-In the Maven we need the Spring Session dependency.Maven will be as follows-
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javainuse.spring</groupId> <artifactId>Spring-Boot-Session-Example</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Spring-Boot-Session-Example</name> <description>Spring-Boot-Session-Example</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-core</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>