Spring Session框架继承零侵入单点登录
一、Redis desktop工具
为了不用在命令行钟使用redis,使对redis的操作更加方便,可以下载Redis desktop工具进行管理
1.1 下载地址
Redis desktop下载地址,提取码为t935
1.2 填写信息,测试连接
[外链图片转存失败(img-r0TSwVIQ-1567250050278)(F:\博客截图\redisdesktop工具.PNG)]
1.3 层级结构说明
如果key中有:分割,则在管理工具中显示层级结构,用于管理redis的命名空间
127.0.0.1:6379> set derrick:jessica:maomaos 0418
OK
二、Spring Session入门
2.1 Maven中央仓库
进入maven中央仓库(https://search.maven.org/)搜索spring-session-data-redis,找到1.2.0.release这个版本进行复制依赖
<!-- spring session 单点登录 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
2.2 配置文件
2.2.1 创建UserSpringSessionController
复制之前的UserController登录、登出、获取用户信息的代码,将RedisShardedPoolUtil相关代码注释掉,换回原生的session代码
@Controller
@RequestMapping("/user/springsession/")
public class UserSpringSessionController {
//将UserService注入
@Autowired
private IUserService iUserService;
//用户登录,ResponseBody让返回的值序列化为json格式
@RequestMapping(value = "login.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<User> login(String username, String password, HttpSession session, HttpServletResponse httpServletResponse){
ServerResponse<User> response = iUserService.login(username,password);
if(response.isSuccess()){
session.setAttribute(Const.CURRENT_USER,response.getData());
System.out.println(session.getId());
//CookieUtil.writeLoginToken(httpServletResponse,session.getId());
//CookieUtil.readLoginToken(httpServletRequest);
//CookieUtil.delLoginToken(httpServletRequest,httpServletResponse);
//RedisShardedPoolUtil.setEx(session.getId(), JsonUtil.obj2String(response.getData()), Const.RedisCacheExtime.REDIS_SESSION_EXTIME);
}
return response;
}
//登出
@RequestMapping(value