Web系统验证码功能实现

概述:验证码这个功能点,不论是桌面扩展性程序和WEB网站都有这个功能,今天我们依靠若依这个项目的成品代码分析验证码的功能点
在这里插入图片描述
逻辑分析:1.验证码中有6个元素
第一个数字**(随机)** 第二个运算符**(随机)** 第三数字**(随机)**
第四个等于号(固定值) 第五个问号(固定值) 第六个计算结果**(随机)**。
2.解决问题,验证码随机!!!提高系统安全性,而不是固定的图片库一开始想法是存很多张图片每个图片都有一个结果。

代码实现(来自若依框架):

package com.ruoyi.web.controller.common;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.google.code.kaptcha.Producer;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.sign.Base64;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.ISysConfigService;

/**
 * 验证码操作处理
 * 
 * @author ruoyi
 */
@RestController
public class CaptchaController
{
    @Resource(name = "captchaProducer")
    private Producer captchaProducer;

    @Resource(name = "captchaProducerMath")
    private Producer captchaProducerMath;

    @Autowired
    private RedisCache redisCache;
    
    // 验证码类型
    @Value("${ruoyi.captchaType}")
    private String captchaType;
    
    @Autowired
    private ISysConfigService configService;
    /**
     * 生成验证码
     */
    @GetMapping("/captchaImage")
    public AjaxResult getCode(HttpServletResponse response) throws IOException
    {
        AjaxResult ajax = AjaxResult.success();
        //获取验证码开关  判断是否要开启验证码
        boolean captchaOnOff = configService.selectCaptchaOnOff();
        //添加验证码key  value
        ajax.put("captchaOnOff", captchaOnOff);

        //如果不要开启验证码 直接返回
        if (!captchaOnOff)
        {
            return ajax;
        }

        // 保存验证码信息
        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;

        String capStr = null, code = null;
        BufferedImage image = null;

        // 生成验证码
        if ("math".equals(captchaType))
        {   //获取验证码中的所有元素
            String capText = captchaProducerMath.createText();
            capStr = capText.substring(0, capText.lastIndexOf("@"));
            code = capText.substring(capText.lastIndexOf("@") + 1);
            image = captchaProducerMath.createImage(capStr);
        }
        else if ("char".equals(captchaType))
        {
            capStr = code = captchaProducer.createText();
            image = captchaProducer.createImage(capStr);
        }

        redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        // 转换流信息写出
        FastByteArrayOutputStream os = new FastByteArrayOutputStream();
        try
        {
            ImageIO.write(image, "jpg", os);
        }
        catch (IOException e)
        {
            return AjaxResult.error(e.getMessage());
        }

        ajax.put("uuid", uuid);
        ajax.put("img", Base64.encode(os.toByteArray()));
        return ajax;
    }
}

获取验证码中的所有元素

@Override
public String getText()
{
    Integer result = 0;
    Random random = new Random();
    //随机生成元素1
    int x = random.nextInt(10);
    //随机生成元素2
    int y = random.nextInt(10);
    //创建存储随机元素的容器
    StringBuilder suChinese = new StringBuilder();
    //随机选择验证码中的操作运算符
    int randomoperands = (int) Math.round(Math.random() * 2);
    if (randomoperands == 0)
    {
        result = x * y;
        suChinese.append(CNUMBERS[x]);
        suChinese.append("*");
        suChinese.append(CNUMBERS[y]);
    }
    else if (randomoperands == 1)
    {
        if (!(x == 0) && y % x == 0)
        {
            result = y / x;
            suChinese.append(CNUMBERS[y]);
            suChinese.append("/");
            suChinese.append(CNUMBERS[x]);
        }
        else
        {
            result = x + y;
            suChinese.append(CNUMBERS[x]);
            suChinese.append("+");
            suChinese.append(CNUMBERS[y]);
        }
    }
    else if (randomoperands == 2)
    {
        if (x >= y)
        {
            result = x - y;
            suChinese.append(CNUMBERS[x]);
            suChinese.append("-");
            suChinese.append(CNUMBERS[y]);
        }
        else
        {
            result = y - x;
            suChinese.append(CNUMBERS[y]);
            suChinese.append("-");
            suChinese.append(CNUMBERS[x]);
        }
    }
    else
    {
        result = x + y;
        suChinese.append(CNUMBERS[x]);
        suChinese.append("+");
        suChinese.append(CNUMBERS[y]);
    }
    //拼接=? 固定元素
    suChinese.append("=?@" + result);
    return suChinese.toString();
}

调用谷歌的验证码工具kaptcha,普通文本生成,通过之前随机生成的验证码text生成对应的image

public BufferedImage createImage(String text) {
    WordRenderer wordRenderer = this.getConfig().getWordRendererImpl();
    GimpyEngine gimpyEngine = this.getConfig().getObscurificatorImpl();
    BackgroundProducer backgroundProducer = this.getConfig().getBackgroundImpl();
    boolean isBorderDrawn = this.getConfig().isBorderDrawn();
    this.width = this.getConfig().getWidth();
    this.height = this.getConfig().getHeight();
    BufferedImage bi = wordRenderer.renderWord(text, this.width, this.height);
    bi = gimpyEngine.getDistortedImage(bi);
    bi = backgroundProducer.addBackground(bi);
    Graphics2D graphics = bi.createGraphics();
    if (isBorderDrawn) {
        this.drawBox(graphics);
    }

    return bi;
}

总结:验证码功能的难点是,逻辑能想通吗,还是比较简单的,获得了验证码的text元素之后,通过调用谷歌的验证码工具kaptcha直接就可以生成

如果没有批评,赞美将毫无意义,欢迎指正批评。

路漫漫其修远兮,吾将上下而求索


版权声明:本文为qq_44815020原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。