网站出租建设,php 网站模板,安装不上wordpress,中国邮政做特产得网站使用Redis存储验证码 验证码需要频繁访问和封信#xff0c;对性能要求高#xff1b;验证码不需要永久保存#xff0c;通常在很短时间内失效#xff1b;分布式部署#xff0c;存在Session共享问题#xff1b; 使用Redis存储登陆凭证 处理每次请求时#xff0c;都要查询用…使用Redis存储验证码 验证码需要频繁访问和封信对性能要求高验证码不需要永久保存通常在很短时间内失效分布式部署存在Session共享问题 使用Redis存储登陆凭证 处理每次请求时都要查询用户登陆凭证访问频率非常高 使用Redis缓存用户信息 处理每次请求时都要根据凭证查询用户信息访问频率非常高。 查询缓存中是否有user如果没有就初始化缓存如果user的信息更新将清除缓存
1. 使用Redis存储验证码
在生成验证码的逻辑中owner由UUID生成并交给Cookie然后将验证码text存到kaptcha:owner键中并设置有效时间为60秒
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里// 验证码的归属ownerString 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键对应的值验证码判断验证码是否正确
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即可。 public MapString,Object login(String username, String password, int expiredSeconds){MapString,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缓存用户信息
// 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。