接口防刷:图形验证码

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

目录

一、背景介绍

二、设计思路

三、核心代码展示

总结


提示:以下是本篇文章正文内容,下面案例可供参考

一、背景介绍

重置密码,会给用户手机号发送一个短信验证码,因为该接口是未登录就可以访问,所以存在防刷风险。还会遇到未申请重置密码的用户,收到了重置密码的短信。

二、设计思路

1、通过用户有效性,能拦截大部分用户不存在的请求,但是会对已存在的用户,发送短信。

2、针对上边这种解决方案,查漏补缺,不能让请求直接穿透我们的逻辑。此时,图形验证码就登场了。

首先,这个东西是免费的,我们可以后台生成一个图形验证码,并返回给前端进行展示,真实的图形验证码存放在缓存中,并设置过期时间。

其次,每次提交的时候,先用手机号校验一层,验证该手机号当前是否有生效中的图形验证码,如果有,继续向下。如果没有,返回提示。

最后,当有生效的图形验证码存在时,校验前端传递的验证码值和缓存中的是否一致。不一致返回提示,一致的话,向下执行核心逻辑,发送短信验证码。

如果出现图形验证码被攻破的场景,我们只需要变换图形验证码的难易程度和复杂度,即可快速升级防刷级别。

三、核心代码展示

1、生成图形验证码的代码示例

public String generateCaptcha() {
    // 创建空白图像
    BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics graphics = image.getGraphics();

    // 设置背景色
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, WIDTH, HEIGHT);

    // 绘制随机字符
    Random random = new Random();
    StringBuilder captcha = new StringBuilder();
    for (int i = 0; i < CHAR_NUM; i++) {
        char c = (char) (random.nextInt(26) + 'A'); // 生成随机大写字母
        captcha.append(c);
        graphics.setFont(new Font("Arial", Font.BOLD, FONT_SIZE));
        graphics.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
        graphics.drawString(String.valueOf(c), i * (WIDTH / CHAR_NUM) + 20, HEIGHT / 2);
    }

    // 添加干扰线和噪点
    graphics.setColor(Color.GRAY);
    for (int i = 0; i < 10; i++) {
        int x1 = random.nextInt(WIDTH);
        int y1 = random.nextInt(HEIGHT);
        int x2 = random.nextInt(WIDTH);
        int y2 = random.nextInt(HEIGHT);
        graphics.drawLine(x1, y1, x2, y2);
    }
    for (int i = 0; i < 100; i++) {
        int x = random.nextInt(WIDTH);
        int y = random.nextInt(HEIGHT);
        graphics.fillOval(x, y, 2, 2);
    }

    // 释放资源
    graphics.dispose();

    return captcha.toString();
}

2、给前端返回图形验证码,用于展示的代码示例,不一样的地方在于,返回的内容不一样。

    public String generateCaptcha() {
    // 创建空白图像
    BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics graphics = image.getGraphics();

    // 设置背景色
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, WIDTH, HEIGHT);

    // 绘制随机字符
    Random random = new Random();
    StringBuilder captcha = new StringBuilder();
    for (int i = 0; i < CHAR_NUM; i++) {
        char c = (char) (random.nextInt(26) + 'A'); // 生成随机大写字母
        captcha.append(c);
        graphics.setFont(new Font("Arial", Font.BOLD, FONT_SIZE));
        graphics.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
        graphics.drawString(String.valueOf(c), i * (WIDTH / CHAR_NUM) + 20, HEIGHT / 2);
    }

    // 添加干扰线和噪点
    graphics.setColor(Color.GRAY);
    for (int i = 0; i < 10; i++) {
        int x1 = random.nextInt(WIDTH);
        int y1 = random.nextInt(HEIGHT);
        int x2 = random.nextInt(WIDTH);
        int y2 = random.nextInt(HEIGHT);
        graphics.drawLine(x1, y1, x2, y2);
    }
    for (int i = 0; i < 100; i++) {
        int x = random.nextInt(WIDTH);
        int y = random.nextInt(HEIGHT);
        graphics.fillOval(x, y, 2, 2);
    }

	// 释放资源
    graphics.dispose();
    // 保存图像到文件
    try {
        File outputfile = new File("captcha.png");
        ImageIO.write(image, "png", outputfile);
        return outputfile.
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 文件的字节流;
}

获取图形验证码的功能,也不用拘泥于一个手机号新生成一个验证码,这个是对资源的浪费。

我们可以预生成一千个、一万个,然后将其存放于缓存中,直接随机从其中读取即可。

那么有人就会问了,这个如果都是固定的,也会被记忆并攻破,可以每隔一段时间,更新一下验证码的库,这样就能保证我们的库是准实时变化的。


总结

动动脑,能防老!

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值