spring-boot 整合 spring-session

  • spring-session 支持集群 session 共享,支持的方式是基于 cookie 或者基于 header。cookie 的限制在于只能在顶级域名下共享,不同的顶级域名之间是无法共享 session 的,所以经过测试 spring-session 无法实现顶级域名的 session 共享。暂时行得通的方式是结合 spring-session,通过辨别同一访问者 ip 来实现 session 共享。

  • 单点登录是把登录后的凭据统一存在 server 端,基于 server 的同一个域名因此可以实现 session 共享。

spring-boot 整合 spring-session 步骤

在 pom.xml 中加入:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-redis</artifactId>
			<version>1.4.7.RELEASE</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-data-redis</artifactId>
		</dependency>

在 application.properties 中加入配置:


# SPRING SESSION REDIS
spring.session.store-type=redis
spring.redis.host={your-ip}
spring.redis.password=abc123456
spring.redis.port=6379
spring.redis.timeout=3000
spring.redis.jedis.pool.max-idle=20
spring.redis.jedis.pool.min-idle=5
#spring.redis.pool.max-active=60

# Maximum age of the session cookie 30 分钟
server.servlet.session.cookie.max-age=1800

加入 spring-session redis 配置:

@Configuration
public class RedisSessionConfig {

	@Bean
	public CustomCookiesSerializer customerCookiesSerializer() {
		CustomCookiesSerializer customerCookiesSerializer = new CustomCookiesSerializer();
		customerCookiesSerializer.setCookieName("JSESSIONID");
		customerCookiesSerializer.setCookiePath("/");//设置 为 '/' 可解决同一台服务器下不同项目直接 session 共享
		return customerCookiesSerializer;
	}

	@Bean
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
		Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
				Object.class);
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		jackson2JsonRedisSerializer.setObjectMapper(om);
		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
		template.setConnectionFactory(redisConnectionFactory);
		template.setKeySerializer(jackson2JsonRedisSerializer);
		template.setValueSerializer(jackson2JsonRedisSerializer);
		template.setHashKeySerializer(jackson2JsonRedisSerializer);
		template.setHashValueSerializer(jackson2JsonRedisSerializer);
		template.afterPropertiesSet();
		return template;
	}

}

使用 CustomCookiesSerializer 覆盖默认的 DefaultCookiesSerializer cookie 读取机制。

redis 安装:

windows 环境:
  1. 下载安装:https://github.com/MicrosoftArchive/redis

  2. 修改 redis.windows.conf 文件

  3. 设置密码:搜索 requirepass,设置为 requirepass 123456

  4. 设置最大内存,默认不设置没有限制,最好是设置为 100 兆:maxmemory 1073741824

  5. 启动 redis: redis-server.exe redis.windows.conf

出现 creating server tcp listening socket 127.0.0.1:6379: bind No error 注释掉配置文件中的 bind 127.0.0.1 或者修改为本机地址

linux 环境:
  1. 下载:http://download.redis.io/releases/redis-4.0.9.tar.gz 编译完即可
  2. 修改配置文件 redis.conf:注释掉 bind,并设置 requirepass(密码) 和最大内存(1073741824), 然后把该文件拷贝到/usr/local/bin
  3. 启动:redis-server /usr/local/bin/redis.conf
常用命令:

登录 redis:

  1. 进入 redis 安装目录下的 src 目录(linux 可全局直接运行):执行 redis-cli -h: {serverIP} -p 6379
  2. 授权: auth {password}
  3. 默认登录进去是0数据库,切换数据库可使用 select {index}

查看所有数据的 key: keys *
清除所有数据:flushall

每一次 session 存储到 redis 内容的 key
spring:session:sessions:expires
spring:session:sessions
spring:session:expirations

参考 spring-session 官方文档

https://projects.spring.io/spring-session/ 右下方 guides

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值