【Java Web】用Redis优化登陆模块

  • 使用Redis存储验证码
    • 验证码需要频繁访问和封信,对性能要求高;
    • 验证码不需要永久保存,通常在很短时间内失效;
    • 分布式部署,存在Session共享问题;
  • 使用Redis存储登陆凭证
    • 处理每次请求时,都要查询用户登陆凭证,访问频率非常高;
  • 使用Redis缓存用户信息
    • 处理每次请求时,都要根据凭证查询用户信息,访问频率非常高。
      • 查询缓存中是否有user;
      • 如果没有就初始化缓存;
      • 如果user的信息更新,将清除缓存;

1. 使用Redis存储验证码

  • 在生成验证码的逻辑中,owner由UUID生成,并交给Cookie;然后将验证码text存到kaptcha:owner键中,并设置有效时间为60秒
java 复制代码
@RequestMapping(path="/kaptcha", method = RequestMethod.GET)
    public void getKaptcha(HttpServletResponse response, HttpSession session){
        // 生成验证码
        String text = kaptchaProducer.createText();
        BufferedImage image = kaptchaProducer.createImage(text);

        // 将验证码存入session
        // session.setAttribute("kaptcha", text);
        // 优化:存到redis里
        // 验证码的归属owner
        String kaptchaOwner = CommunityUtil.generateUUID();
        Cookie cookie = new Cookie("kaptchaOwner",kaptchaOwner);
        cookie.setMaxAge(60);
        cookie.setPath(context_path);
        response.addCookie(cookie);

        String redisKey = RedisKeyUtil.getKaptchaKey(kaptchaOwner);
        redisTemplate.opsForValue().set(redisKey, text, 60, TimeUnit.SECONDS);

        // 将图片输出给浏览器
        response.setContentType("image/png");
        try{
            OutputStream os = response.getOutputStream();
            ImageIO.write(image, "png", os);
        } catch (IOException e) {
            logger.error("响应验证码失败:"+e.getMessage());
        }
    }
  • 在登陆功能中,键从cookie中去哪出,然后从redis中获取kaptcha:kaptchaOwner键对应的值(验证码),判断验证码是否正确;
java 复制代码
@RequestMapping(path = "/login", method = RequestMethod.POST)
    public String login(String username, String password, String code, boolean rememberme,
                        Model model, HttpSession session, HttpServletResponse response, @CookieValue("kaptchaOwner") String kaptchaOwner){
        // 从Session中取并检查验证码 --> 优化:从Redis中取
        // String kaptcha = (String) session.getAttribute("kaptcha");
        String kaptcha = null;
        if(StringUtils.isNotBlank(kaptchaOwner)){
            String redisKey = RedisKeyUtil.getKaptchaKey(kaptchaOwner);
            kaptcha = (String) redisTemplate.opsForValue().get(redisKey);
        }

        if(StringUtils.isBlank(kaptcha) || StringUtils.isBlank(code) || !kaptcha.equals(code)){
            model.addAttribute("codeMsg","验证码不正确");
            return "/site/login";
        }
        ......
        ......
        ......
        ......

2. 使用Redis存储登陆凭证

各个逻辑验证通过后,将生成一个用户凭证(UUID生成),存到Redis中,并设置生存周期。在后续需要验证登陆逻辑时,从Redis中get即可。

java 复制代码
 public Map<String,Object> login(String username, String password, int expiredSeconds){
        Map<String,Object> map = new HashMap<>();
        // 空值判断
        ....
        ....
        
        // 账号密码都不为空,验证合法性
        // 验证账号合法性
        .....
        .....
        
        // 验证密码
        ....
        ....
        
        // 生成登陆凭证
        LoginTicket loginTicket = new LoginTicket();
        loginTicket.setUserId(user.getId());
        loginTicket.setTicket(CommunityUtil.generateUUID());
        loginTicket.setStatus(0);
        loginTicket.setExpired(new Date(System.currentTimeMillis() + 1000 * 60 * 10));
        loginTicketMapper.insertLoginTicket(loginTicket);

        String redisKey = RedisKeyUtil.getTicketKey(loginTicket.getTicket());
        redisTemplate.opsForValue().set(redisKey,loginTicket);

        map.put("ticket", loginTicket.getTicket());
        return map;
    }

3. 使用Redis缓存用户信息

java 复制代码
// 1. 优先从缓存中取值
    public User getCache(int userId){
        String redisKey = RedisKeyUtil.getUserKey(userId);
        return (User) redisTemplate.opsForValue().get(redisKey);
    }
    // 2. 如果取不到就初始化缓存
    public 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. 数据变更时清除缓存数据
    public void clearCache(int userId){
        String redisKey = RedisKeyUtil.getUserKey(userId);
        redisTemplate.delete(redisKey);
    }
  • 当获取user的时候,例如根据userId获取用户信息,先判断缓存中查有没有user:userId这个键。如果有,就从缓存中返回User;如果没有,就初始化缓存,将User信息写入user:userId。
  • 每次用户信息修改后,例如更改密码后、退出登录修改ticket后、修改用户激活状态后等,多需要做一次clearCache。
相关推荐
nothingbutluck4647 分钟前
2025.4.10 html有序、无序、定义列表、音视频标签
前端·html·音视频
小五Z23 分钟前
Redis--事务
redis·分布式·后端·缓存
牛马baby27 分钟前
Springboot 自动装配原理是什么?SPI 原理又是什么?
java·spring boot·后端
爱上python的猴子39 分钟前
chrome中的copy xpath 与copy full xpath的区别
前端·chrome
小小深1 小时前
了解JVM
java·jvm
Sunlight_7771 小时前
第五章 SQLite数据库:1、SQLite 基础语法及使用案例
java·linux·服务器·jvm·数据库·tcp/ip·sqlite
JhonKI1 小时前
【从零实现高并发内存池】内存池整体框架设计 及 thread cache实现
java·redis·缓存
何似在人间5751 小时前
SpringAI+DeepSeek大模型应用开发——4 对话机器人
java·机器人·大模型应用开发·spring ai
Lysun0011 小时前
dispaly: inline-flex 和 display: flex 的区别
前端·javascript·css
山禾女鬼0012 小时前
Vue 3 自定义指令
前端·javascript·vue.js