找回密码
 立即注册
查看: 3|回复: 0

3,若依缓存使用

[复制链接]

280

主题

6

精华

284

金币

技术维护QQ:515138

积分
611
发表于 2 小时前 | 显示全部楼层 |阅读模式
1.png

  1. package com.jinhei.web.controller.common;
  2. import java.awt.image.BufferedImage;
  3. import java.io.IOException;
  4. import java.util.concurrent.TimeUnit;
  5. import jakarta.annotation.Resource;
  6. import javax.imageio.ImageIO;
  7. import jakarta.servlet.http.HttpServletResponse;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.util.FastByteArrayOutputStream;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import com.google.code.kaptcha.Producer;
  13. import com.jinhei.common.config.JinHeiConfig;
  14. import com.jinhei.common.constant.CacheConstants;
  15. import com.jinhei.common.constant.Constants;
  16. import com.jinhei.common.core.domain.AjaxResult;
  17. import com.jinhei.common.core.redis.RedisCache;
  18. import com.jinhei.common.utils.sign.Base64;
  19. import com.jinhei.common.utils.uuid.IdUtils;
  20. import com.jinhei.system.service.ISysConfigService;
  21. /**
  22. * 验证码操作处理
  23. *
  24. * @author jinhei.com
  25. */
  26. @RestController
  27. public class CaptchaController
  28. {
  29.     @Resource(name = "captchaProducer")
  30.     private Producer captchaProducer;
  31.     @Resource(name = "captchaProducerMath")
  32.     private Producer captchaProducerMath;
  33.     @Autowired
  34.     private RedisCache redisCache;
  35.    
  36.     @Autowired
  37.     private ISysConfigService configService;
  38.     /**
  39.      * 生成验证码
  40.      */
  41.     @GetMapping("/captchaImage")
  42.     public AjaxResult getCode(HttpServletResponse response) throws IOException
  43.     {
  44.         /*
  45.          *
  46.          * ajax 统一结果集对象,统一返回结果
  47.          */
  48.         AjaxResult ajax = AjaxResult.success();
  49.         boolean captchaEnabled = configService.selectCaptchaEnabled(); // 是否开启验证码
  50.         ajax.put("captchaEnabled", captchaEnabled); // 把是否开启验证码的结果告知前端
  51.         if (!captchaEnabled)
  52.         {
  53.             return ajax;
  54.         }
  55.         // 保存验证码信息
  56.         String uuid = IdUtils.simpleUUID();
  57.         String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
  58.         String capStr = null, code = null;
  59.         BufferedImage image = null;
  60.         // 生成验证码
  61.         String captchaType = JinHeiConfig.getCaptchaType();
  62.         if ("math".equals(captchaType))
  63.         {
  64.             String capText = captchaProducerMath.createText();
  65.             capStr = capText.substring(0, capText.lastIndexOf("@"));
  66.             code = capText.substring(capText.lastIndexOf("@") + 1);
  67.             image = captchaProducerMath.createImage(capStr);
  68.         }
  69.         else if ("char".equals(captchaType))
  70.         {
  71.             capStr = code = captchaProducer.createText();
  72.             image = captchaProducer.createImage(capStr);
  73.         }
  74.         // 存入缓存中
  75.         redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
  76.         // 转换流信息写出
  77.         FastByteArrayOutputStream os = new FastByteArrayOutputStream();
  78.         try
  79.         {
  80.             ImageIO.write(image, "jpg", os);
  81.         }
  82.         catch (IOException e)
  83.         {
  84.             return AjaxResult.error(e.getMessage());
  85.         }
  86.         ajax.put("uuid", uuid);
  87.         ajax.put("img", Base64.encode(os.toByteArray()));
  88.         return ajax;
  89.     }
  90. }
复制代码




上一篇:2,若依验证码实现
下一篇:4,登录功能
网站建设,公众号小程序开发,系统定制,软件App开发,技术维护【联系我们】手机/微信:17817817816 QQ:515138

QQ|Archiver|自丢网 ( 粤ICP备2024252464号-1 )

GMT+8, 2026-3-12 17:57

专注于网站建设,公众号小程序制作,商城小程序,系统定制,软件App开发

【联系我们】手机/微信:17817817816 QQ:515138

快速回复 返回顶部 返回列表