Spring Session with Redis Tutorial
Introduction
Spring Session is a project that provides an API and implementations for the server-side HTTP session. It allows you to manage session data in a distributed and scalable manner. One of the most popular backends for Spring Session is Redis. This tutorial will guide you through setting up Spring Session with Redis, demonstrating how to configure your application, and showing how to store session data in Redis.
Prerequisites
Before you begin, ensure you have the following:
- Java Development Kit (JDK) 11 or later
- Apache Maven installed
- Redis server installed and running
Setting Up Your Project
To get started, create a new Maven project. You can do this using your IDE or by running the following command:
Once the project is created, navigate to the `pom.xml` file and add the necessary dependencies for Spring Session and Redis:
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
Configuration
Next, you need to configure Spring Session to use Redis. Create a configuration class named `RedisConfig.java`:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@Configuration
@EnableRedisHttpSession
public class RedisConfig {
@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory());
return template;
}
}
Using Spring Session
With the configuration in place, you can now use Spring Session to manage HTTP sessions. For demonstration, create a simple controller `SessionController.java` that will allow you to store and retrieve session attributes:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;
@RestController
@SessionAttributes("message")
public class SessionController {
@GetMapping("/setSession")
public String setSession(@RequestParam String message, Model model) {
model.addAttribute("message", message);
return "Session attribute set";
}
@GetMapping("/getSession")
public String getSession(@SessionAttribute("message") String message) {
return "Session message: " + message;
}
}
Running Your Application
To run your application, navigate to the root directory of your project and execute:
You can now access your application at http://localhost:8080/setSession?message=Hello
to set a session attribute and http://localhost:8080/getSession
to retrieve it.
Conclusion
In this tutorial, you learned how to set up Spring Session with Redis, configure your application, and manage session attributes. Spring Session provides a powerful way to handle session data in a distributed environment, making it an excellent choice for modern web applications.