SpringBoot与SpringSession集成

coding

1. 参考资料

  • https://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot-redis.html
  • https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#boot-features-session

2. 环境

Java: jdk1.8.0_144
Spring Boot: 2.0.1.RELEASE

3. 配置

本文前提是已使用Spring Boot搭建了一个Web应用,在此基础上集成Spring Session以使用Redis管理该应用的session

  • 引入依赖包
    <dependency>

<groupId>org.springframework.session</groupId>

<artifactId>spring-session-data-redis</artifactId>

</dependency>

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

</dependency>

  • Spring Session配置
spring.session.store-type=redis

配置该属性后,应用启动时将创建名为springSessionRepositoryFilter的过滤器,它负责在后台把javax.servlet.http.HttpSession的实现替换为Spring Session,更多的配置示例

spring.session.redis.flush-mode=on-save

spring.session.redis.namespace=spring:session

server.servlet.session.timeout=1800

  • Redis连接配置
spring.redis.host=127.0.0.1

spring.redis.password=123456

spring.redis.port=6379

应用将自动创建RedisConnectionFactory并连接目标Redis Server

4. Redis存储结构

对象结构存入javax.servlet.http.HttpSession,最终需要以字节或者字符的形式存入Redis,故可以指定序列化与反序列方法(例如org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer),若未指定则使用JDK自带的java.io.Serializable,以下面的类为例说明执行httpSession.setAttribute("verify", verify)在使用不同序列化方法时的表现

public class Verify {

private String code;// 如 1 + 2

private Integer value;// 如 3

public String getCode() {

return code;

}

public void setCode(String code) {

this.code = code;

}

public Integer getValue() {

return value;

}

public void setValue(Integer value) {

this.value = value;

}

}

  • java.io.Serializable

此时Verify类必须实现java.io.Serializable接口,否则抛出异常

Caused by: java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [org.*.Verify]

at org.springframework.core.serializer.DefaultSerializer.serialize(DefaultSerializer.java:43)

at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:63)

... 37 common frames omitted

Redis中的结构示例:

127.0.0.1:6379> keys *

1) "spring:session:sessions:d850410a-557d-4b01-b194-9af0c3336517"

2) "spring:session:expirations:1529485860000"

3) "spring:session:sessions:expires:d850410a-557d-4b01-b194-9af0c3336517"

127.0.0.1:6379> HGETALL spring:session:sessions:d850410a-557d-4b01-b194-9af0c3336517

1) "maxInactiveInterval"

2) "\xac\xed\x00\x05sr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\a\b"

3) "sessionAttr:verify"

4) "\xac\xed\x00\x05sr\x00 org.lxp.dailylog.web.util.Verify\x00\x00\x00\x00\x00\x00\x00\x01\x02\x00\x02L\x00\x04codet\x00\x12Ljava/lang/String;L\x00\x05valuet\x00\x13Ljava/lang/Integer;xpt\x00\a1 + 5=?sr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x00\x06"

5) "creationTime"

6) "\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x01d\x1cZSC"

7) "lastAccessedTime"

8) "\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x01d\x1cZSC"

127.0.0.1:6379>

  • org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer
import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration

public class JedisConfig {

@Bean

public RedisSerializer<Object> springSessionDefaultRedisSerializer() {

return new Jackson2JsonRedisSerializer<>(Object.class);

}

}

Redis中的结构示例:

127.0.0.1:6379> keys *

1) "spring:session:expirations:1529486220000"

2) "spring:session:sessions:expires:8a804a79-862e-4e05-9fc0-e8db7cbf33f9"

3) "spring:session:sessions:8a804a79-862e-4e05-9fc0-e8db7cbf33f9"

127.0.0.1:6379> HGETALL spring:session:sessions:8a804a79-862e-4e05-9fc0-e8db7cbf33f9

1) "lastAccessedTime"

2) "1529484404321"

3) "sessionAttr:verify"

4) "{\"code\":\"8 + 10=?\",\"value\":18}"

5) "maxInactiveInterval"

6) "1800"

7) "creationTime"

8) "1529484404321"

以上是 SpringBoot与SpringSession集成 的全部内容, 来源链接: utcz.com/z/509383.html

回到顶部