SpringセッションでセッションをRedisへ保存する
#Spring #Spring-Session #Redis
サンプルコード
https://github.com/yuizho/spring-session-sandbox/tree/manage-session-with-redis
https://gyazo.com/ac695a5f42620d0e1d29cd3854188a00
必要な設定
わりと少しの設定ですんなりとRedisにセッション情報を逃がすことができた。
pom.xml
code: xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
application.properties
https://spring.pleiades.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#security-properties
code: properties
spring.session.store-type=redis
spring.session.namespace=appname
# redis のホスト名を設定
spring.redis.host=redis
spring.redis.port=6379
Sessionオブジェクトのシリアライズ
通常はセッションに入れるクラスがSerializableになっていれば問題なく、Redisへ保存される
ちゃんと試していないが、JacksonなどでJSONにシリアライズするようなやり方もあるっぽい
https://qiita.com/kazuki43zoo/items/8123906809632503cc21#redisに格納するセッション情報のシリアライズ方法の変更