一、使用Redis存储验证码

  • 验证码需要频繁地访问与刷新,对性能要求较高
  • 验证码不需要永久保存,通常在很短的时间内就会失效
  • 分布式部署时,存在Session共享的问题

以登录模块验证码(一分钟失效)为例:

1. 在登录之前,需要有一个凭证来短暂地映射该用户,对此,我们随机生成一个字符串来作为Redis的key,并将其存入cookie中以便登录时获取

// 验证码的归属
String kaptchaOwner = CommunityUtil.generateUUID();// 自定义的获取随机字符串的方法
Cookie cookie = new Cookie("kaptchaOwner", kaptchaOwner);
cookie.setMaxAge(60);// 验证码失效时间1分钟
cookie.setPath(contextPath);// contextPath为项目路径
response.addCookie(cookie);

2. 然后,将验证码存入Redis中(随机生成的字符串作为Redis的key)

// 将验证码存入Redis
String redisKey = RedisKeyUtil.getKaptchaKey(kaptchaOwner);// RedisKeyUtil为自定义的获取/拼装Redis Key的一个类
redisTemplate.opsForValue().set(redisKey, text, 60, TimeUnit.SECONDS);// text为随机验证码

3. 登录时,获取之前存入Redis的验证码

可以通过@CookieValue注解获取之前存入Cookie中的凭证(随机字符串)

@CookieValue("kaptchaOwner") String kaptchaOwner
// 检查验证码
String kaptcha = null;
if(StringUtils.isNoneBlank(kaptchaOwner)){
    String redisKey = RedisKeyUtil.getKaptchaKey(kaptchaOwner);
    kaptcha = (String) redisTemplate.opsForValue().get(redisKey);
}

二、使用Redis存储登录凭证

  • 处理每次请求时,都要查询用户的登录凭证,访问的频率非常高

1. 登录时,生成登录凭证,并以登录凭证(随机字符串)作为Redis的Key存入Redis

// 生成登录凭证
LoginTicket loginTicket = new LoginTicket();
loginTicket.setUserId(user.getId());
loginTicket.setTicket(CommunityUtil.generateUUID());
loginTicket.setStatus(0);
loginTicket.setExpired(new Date(System.currentTimeMillis() + expiredSeconds * 1000));
// loginTicketMapper.insertLoginTicket(loginTicket);

String redisKey = RedisKeyUtil.getTicketKey(loginTicket.getTicket());
redisTemplate.opsForValue().set(redisKey, loginTicket);// loginTicket会自动序列化为String类型

2. 退出登录时,将登录状态改为1,即登录凭证失效

// loginTicketMapper.updateStatus(ticket,1);
String redisKey = RedisKeyUtil.getTicketKey(ticket);
LoginTicket loginTicket = (LoginTicket) redisTemplate.opsForValue().get(redisKey);
loginTicket.setStatus(1);
redisTemplate.opsForValue().set(redisKey, loginTicket);

3. 在拦截器的作用下,每次刷新页面,查询登录凭证

// return loginTicketMapper.selectByTicket(ticket);
String redisKey = RedisKeyUtil.getTicketKey(ticket);
return (LoginTicket) redisTemplate.opsForValue().get(redisKey);

 三、使用Redis缓存用户信息

  • 处理每次请求时,都要根据凭证查询用户信息,访问的频率非常高。

1. 每次访问user信息时,优先从缓存中取值

private User getCache(int userId) {
    String redisKey = RedisKeyUtil.getUserKey(userId);
    return (User) redisTemplate.opsForValue().get(redisKey);
}

2. 从缓存中取不到时,初始化缓存数据(从Mysql中取)

private User initCache(int userId) {
    User user = userMapper.selectById(userId);
    String redisKey = RedisKeyUtil.getUserKey(userId);
    redisTemplate.opsForValue().set(redisKey, user, 3600, TimeUnit.SECONDS);
    return user;
}

3. 数据变更时,清除缓存数据

private void clearCache(int userId) {
    String redisKey = RedisKeyUtil.getUserKey(userId);
    redisTemplate.delete(redisKey);
}

注:数据变更时,若选择修改缓存中的数据,可能会引起并发等问题,所以,我们直接选择删除缓存中的数据,当再次访问此数据时初始化缓存即可。 

从Redis缓存中取用户信息示例:

public User findUserById(int id){
    User user = getCache(id);// 先从缓存中取值
    if(user == null) {
        user = initCache(id);// 取不到时,初始化缓存数据
    }
    return user;
}

数据变更清除缓存数据示例:

public int updateHeader(int userId,String headerUrl){
    int rows = userMapper.updateHeader(userId, headerUrl);// 更改用户信息
    clearCache(userId);// 更改用户信息后清除缓存数据
    return rows;
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐