SpringSession使用redis存储HttpSession(共享session)

本文介绍了如何使用SpringSession结合Redis实现HttpSession的分布式存储。SpringSession提供多种存储方式,如Redis、JVM Map等,以解决分布式集群中的session共享问题。文中详细讲解了在Redis环境下设置和配置SpringSession的步骤,并通过测试验证了HttpSession数据成功存储到Redis中。
摘要由CSDN通过智能技术生成

web开发中session一直都是做分布式集群应用时需要解决的一个难题,过去解决这个难题的一般方式是从serlvet容器上解决,而现在使用spring session能够很容易的把session存储到第三方存储容器,框架提供了redis、jvm的map、mongo、gemfire、hazelcast、jdbc等多种存储session的容器的方式。 关于SpringSession更多的介绍以及功能,可以到SpringSession的官网上查看,SpringSession官方网站中也给出了简单快速的入门案例,Spring Session - HttpSession (Quick Start),本文是在此基础上实践后给予总结,以及分享一下我在学习springsession过程中遇到的问题以及解决方法。

Spring Session+Redis案例

1.Redis

Redis版本需要在2.8+以上,本案例使用的是redis-4.0.1.tar.gz(下载地址:redis4.0.1),在CentOS6.5下搭建了redis环境,运行并且监听默认的6379端口。

为了能让redis正常运行起来并且顺利使用客户端连接之前需要注意几点:

启动之前需要修改redis目录下的redis.conf配置文件:

(1)远程主机连接redis服务器

# bind 127.0.0.1 ::1   首先要注释掉这个绑定的ip地址,这是让其他机子能通过网络连接redis的前提。

(2)连接redis服务器的登录问题

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
# "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
#protected-mode yes
#daemonize no
#requirepass 677714
protected-mode no

这是连接redis服务器时是不是需要登录密码的设置,默认protected-mode的值为true,需要自己加上requirepass作为登录密码。以后客户端连接该服务器时需要使用-a passpw来指定密码验证登录。假设说redis服务器不需要使用密码进行登录,直接将p

使用SpringBoot框架结合MyBatis实现Session共享和单点登录可以借助SpringSessionRedis来实现。 首先,需要配置SpringSession使用Redis作为存储方式。可以在SpringBoot的配置文件中添加以下配置: ``` spring.session.store-type=redis spring.session.redis.namespace=spring:session spring.redis.host=127.0.0.1 spring.redis.port=6379 ``` 这样配置后,SpringSession会自动将session信息存储Redis中。 接着,在登录验证成功后,将用户信息存储Redis中,并将该用户的唯一标识存储到当前Session的属性中,以便后续验证是否登录。例如: ``` @RequestMapping("/login") public String login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) { // 验证用户名和密码 // ... // 验证通过后,将用户信息存储Redis中,并设置Session属性 redisTemplate.opsForHash().put("user:" + username, "username", username); session.setAttribute("username", username); return "success"; } ``` 在后续的请求中,可以通过拦截器或过滤器来验证Session是否有效。例如: ``` @Component public class SessionInterceptor implements HandlerInterceptor { @Autowired private RedisTemplate<String, Object> redisTemplate; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); String username = (String) session.getAttribute("username"); if (StringUtils.isEmpty(username)) { response.sendRedirect("/login"); return false; } String storedUsername = (String) redisTemplate.opsForHash().get("user:" + username, "username"); if (!StringUtils.equals(storedUsername, username)) { response.sendRedirect("/login"); return false; } return true; } } ``` 以上代码片段展示了如何通过拦截器验证Session的有效性。首先从当前Session中获取用户名,如果为空则重定向到登录页面。然后从Redis中获取存储的用户名,如果与当前用户名不匹配,则重定向到登录页面。 这样就实现了SpringBoot、MyBatis、SpringSessionRedis共同完成Session共享和单点登录的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值