当前位置: 代码网 > it编程>编程语言>Java > SpringBoot实现发送验证码功能(图片验证码)

SpringBoot实现发送验证码功能(图片验证码)

2024年07月02日 Java 我要评论
提示:本次内容主要学习如何做一个发送验证码和识别验证码的功能前言提示:本次内容主要学习如何做一个发送验证码和识别验证码的功能例如:随着现在互联网的不断发展,web发展可谓是越来越好,但是在这种情况之下

提示:本次内容主要学习如何做一个发送验证码和识别验证码的功能

前言

提示:本次内容主要学习如何做一个发送验证码和识别验证码的功能

例如:随着现在互联网的不断发展,web发展可谓是越来越好,但是在这种情况之下,就会出现很多不守规矩的人,可能会利用爬虫来不断地去破解你的用户,此时验证码的作用就发挥出来了,可以有效地阻止一些不法分子来破解你的网站

一、图片验证码是什么?

图片验证码(captcha)是一种通过生成图片来验证用户身份的技术,用于区分人类用户和自动化程序。其主要目的是防止机器人的恶意访问和攻击,保护网站的安全。

二、使用步骤

1.创建验证码生成

代码如下(示例):这个代码主要来源于程序员老罗的教程

package com.easybbs.entity.dto;
import javax.imageio.imageio;
import java.awt.*;
import java.awt.image.bufferedimage;
import java.io.ioexception;
import java.io.outputstream;
import java.util.random;
public class createimagecode {
    // 图片的宽度。
    private int width = 160;
    // 图片的高度。
    private int height = 40;
    // 验证码字符个数
    private int codecount = 4;
    // 验证码干扰线数
    private int linecount = 20;
    // 验证码
    private string code = null;
    // 验证码图片buffer
    private bufferedimage buffimg = null;
    random random = new random();
    public createimagecode() {
        creatimage();
    }
    public createimagecode(int width, int height) {
        this.width = width;
        this.height = height;
        creatimage();
    }
    public createimagecode(int width, int height, int codecount) {
        this.width = width;
        this.height = height;
        this.codecount = codecount;
        creatimage();
    }
    public createimagecode(int width, int height, int codecount, int linecount) {
        this.width = width;
        this.height = height;
        this.codecount = codecount;
        this.linecount = linecount;
        creatimage();
    }
    // 生成图片
    private void creatimage() {
        int fontwidth = width / codecount;// 字体的宽度
        int fontheight = height - 5;// 字体的高度
        int codey = height - 8;
        // 图像buffer
        buffimg = new bufferedimage(width, height, bufferedimage.type_int_rgb);
        graphics g = buffimg.getgraphics();
        //graphics2d g = buffimg.creategraphics();
        // 设置背景色
        g.setcolor(getrandcolor(200, 250));
        g.fillrect(0, 0, width, height);
        // 设置字体
        //font font1 = getfont(fontheight);
        font font = new font("fixedsys", font.bold, fontheight);
        g.setfont(font);
        // 设置干扰线
        for (int i = 0; i < linecount; i++) {
            int xs = random.nextint(width);
            int ys = random.nextint(height);
            int xe = xs + random.nextint(width);
            int ye = ys + random.nextint(height);
            g.setcolor(getrandcolor(1, 255));
            g.drawline(xs, ys, xe, ye);
        }
        // 添加噪点
        float yawprate = 0.01f;// 噪声率
        int area = (int) (yawprate * width * height);
        for (int i = 0; i < area; i++) {
            int x = random.nextint(width);
            int y = random.nextint(height);
            buffimg.setrgb(x, y, random.nextint(255));
        }
        string str1 = randomstr(codecount);// 得到随机字符
        this.code = str1;
        for (int i = 0; i < codecount; i++) {
            string strrand = str1.substring(i, i + 1);
            g.setcolor(getrandcolor(1, 255));
            // g.drawstring(a,x,y);
            // a为要画出来的东西,x和y表示要画的东西最左侧字符的基线位于此图形上下文坐标系的 (x, y) 位置处
            g.drawstring(strrand, i * fontwidth + 3, codey);
        }
    }
    // 得到随机字符
    private string randomstr(int n) {
        string str1 = "abcdefghjkmnopqrstuvwxyzabcdefghjkmnopqrstuvwxyz234567890";
        string str2 = "";
        int len = str1.length() - 1;
        double r;
        for (int i = 0; i < n; i++) {
            r = (math.random()) * len;
            str2 = str2 + str1.charat((int) r);
        }
        return str2;
    }
    // 得到随机颜色
    private color getrandcolor(int fc, int bc) {// 给定范围获得随机颜色
        if (fc > 255) fc = 255;
        if (bc > 255) bc = 255;
        int r = fc + random.nextint(bc - fc);
        int g = fc + random.nextint(bc - fc);
        int b = fc + random.nextint(bc - fc);
        return new color(r, g, b);
    }
    /**
     * 产生随机字体
     */
    private font getfont(int size) {
        random random = new random();
        font font[] = new font[5];
        font[0] = new font("ravie", font.plain, size);
        font[1] = new font("antique olive compact", font.plain, size);
        font[2] = new font("fixedsys", font.plain, size);
        font[3] = new font("wide latin", font.plain, size);
        font[4] = new font("gill sans ultra bold", font.plain, size);
        return font[random.nextint(5)];
    }
    // 扭曲方法
    private void shear(graphics g, int w1, int h1, color color) {
        shearx(g, w1, h1, color);
        sheary(g, w1, h1, color);
    }
    private void shearx(graphics g, int w1, int h1, color color) {
        int period = random.nextint(2);
        boolean bordergap = true;
        int frames = 1;
        int phase = random.nextint(2);
        for (int i = 0; i < h1; i++) {
            double d = (double) (period >> 1) * math.sin((double) i / (double) period + (6.2831853071795862d * (double) phase) / (double) frames);
            g.copyarea(0, i, w1, 1, (int) d, 0);
            if (bordergap) {
                g.setcolor(color);
                g.drawline((int) d, i, 0, i);
                g.drawline((int) d + w1, i, w1, i);
            }
        }
    }
    private void sheary(graphics g, int w1, int h1, color color) {
        int period = random.nextint(40) + 10; // 50;
        boolean bordergap = true;
        int frames = 20;
        int phase = 7;
        for (int i = 0; i < w1; i++) {
            double d = (double) (period >> 1) * math.sin((double) i / (double) period + (6.2831853071795862d * (double) phase) / (double) frames);
            g.copyarea(i, 0, 1, h1, 0, (int) d);
            if (bordergap) {
                g.setcolor(color);
                g.drawline(i, (int) d, i, 0);
                g.drawline(i, (int) d + h1, i, h1);
            }
        }
    }
    public void write(outputstream sos) throws ioexception {
        imageio.write(buffimg, "png", sos);
        sos.close();
    }
    public bufferedimage getbuffimg() {
        return buffimg;
    }
    public string getcode() {
        return code.tolowercase();
    }
}

2.生成验证码

代码如下(示例):

@requestmapping("/checkcode")
    public void checkcode(httpservletresponse response, httpsession session) throws ioexception {
        // 创建一个验证码对象,参数分别指定验证码的宽度、高度、字符数量和干扰线数量。
        createimagecode vcode = new createimagecode(130, 38, 5, 10);
        // 设置响应头,确保浏览器不缓存验证码图像。
        response.setheader("pragma","no-cache");
        response.setheader("cache-control","no-cache");
        response.setdateheader("expires", 0);
        // 设置响应类型为jpeg图像。
        response.setcontenttype("image/jpeg");
        // 生成验证码字符串。
        string code = vcode.getcode();
        // 根据类型将验证码存储在不同的session属性中。
        session.setattribute("checkcodekey",code);
        // 将生成的验证码图像写入响应输出流。
        vcode.write(response.getoutputstream());
    }

具体识别的时候可以从session里面取到,具体情况具体对待吧

总结

这里主要是记录了一下如何生成图片验证码

到此这篇关于springboot实现发送验证码功能的文章就介绍到这了,更多相关springboot发送验证码内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com