前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >生成图形验证码是如此简单

生成图形验证码是如此简单

作者头像
用户8889406
发布2023-03-05 15:25:14
6010
发布2023-03-05 15:25:14
举报
文章被收录于专栏:小呙同学

依赖

代码语言:javascript
复制
<!-- kaptcha验证码-->
<dependency>
    <groupId>com.github.penggle</groupId>
    <artifactId>kaptcha</artifactId>
    <version>2.3.2</version>
</dependency>

获取图形验证码

前端传入uuid,后端通过uuid作为redis缓存key

代码语言:javascript
复制
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.ruoyi.common.core.domain.RApp;
import com.ruoyi.common.core.utils.KeyUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.shlz.common.utils.redis.RedisConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;


/**
 * 图形验证码控制器
 *
 * @author xGuo
 * @date 2022/03/25
 */
@RestController
@RequestMapping("/api/code/")
@Slf4j
public class VerifyCodeController {
    @Autowired
    private  RedisService redisService;
    @Autowired
    private  Producer producer;

    /**
     * 获取验证码
     *
     * @param request  请求
     * @param response 响应
     * @param uuid     uuid
     * @return {@link ModelAndView}
     * @throws IOException ioexception
     */
    @GetMapping("getCode")
    public ModelAndView getCode(HttpServletRequest request, HttpServletResponse response
            , @RequestParam(value = "uuid") String uuid) throws IOException {

        ModelAndView mv = new ModelAndView();

        if (StringUtils.isEmpty(uuid)) {
            uuid = KeyUtils.randomUUID(false);
        }
        mv.addObject("uuid", uuid);
        String verifyCodeKey = RedisConst.RedisKeyPrefix.VERIFY_CODE + uuid;

        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        String capText = producer.createText();
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
        //存入redis
        redisService.setCacheObject(verifyCodeKey, capText
                , RedisConst.RedisCacheExTime.REDIS_VERIFY_CODE_EXTIME, TimeUnit.SECONDS);

        BufferedImage bi = producer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
        return null;
    }

校验验证码

代码语言:javascript
复制
/**
 * 校验验证码
 *
 * @param code    验证码
 * @param uuid    uuid
 * @return {@link RApp}<{@link ?}>
 */
@GetMapping("checkCode")
public RApp<?> checkCode(@RequestParam(value = "code") String code
        , @RequestParam(value = "uuid") String uuid) {
    String original = redisService.getCacheObject(RedisConst.RedisKeyPrefix.VERIFY_CODE + uuid);
    if (StringUtils.isNotEmpty(code)) {
        if (code.equalsIgnoreCase(original)) {
            return RApp.createBySuccessMsg("验证码通过");
        }
    }
    return RApp.createByErrorMsg("验证码失败");
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 依赖
  • 获取图形验证码
  • 校验验证码
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档