Java web实现动态图片验证码

验证码
  • 防止恶意表单注册
生成验证码图片
  • 定义宽高
int width = 100;
int height = 50;
  • 使用BufferedImage再内存中生成图片
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  • 绘制背景和边框
Graphics g = image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
  • 创建随机字符集和随机数对象
//字符集
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgjijklmnopqrstuvwxyz";

//随机数
Random ran = new Random();
  • 创建随机颜色生成方法
private Color getRandomColor(Random random) {
    //获取随机颜色
    int colorIndex = random.nextInt(3);
    switch (colorIndex) {
        case 0:
            return Color.BLUE;
        case 1:
            return Color.GREEN;
        case 2:
            return Color.RED;
        case 3:
            return Color.YELLOW;
        default:
            return Color.MAGENTA;
    }
}
  • 绘制验证码字符
//绘制验证码
for (int i = 0; i < 4; i++) {
    //获取随机字符
    int index = ran.nextInt(str.length());
    char ch = str.charAt(index);
    //获取随机色
    Color randomColor = getRandomColor(ran);
    g.setColor(randomColor);
    //设置字体
    Font font = new Font("宋体", Font.BOLD, height / 2);
    g.setFont(font);
    //写入验证码
    g.drawString(ch + "", (i == 0) ? width / 4 * i + 2 : width / 4 * i, height - height / 4);
}
  • 绘制干扰线
//干扰线
for (int i = 0; i < 10; i++) {
    int x1 = ran.nextInt(width);
    int x2 = ran.nextInt(width);
    int y1 = ran.nextInt(height);
    int y2 = ran.nextInt(height);
    Color randomColor = getRandomColor(ran);
    g.setColor(randomColor);
    g.drawLine(x1, x2, y1, y2);
}
  • 使用ImageIO输出图片
ImageIO.write(image, "jpg", resp.getOutputStream());
  • 成果图

在这里插入图片描述

实现刷新效果
  • 新建html页面
  • 使用img标签实现图片展示
<img id="identcode" src="identcode">
<a id="refesh" href="">看不清,换一张</a>
  • 使用js实现刷新效果
//点击图片时
var img = document.getElementById("identcode");
img.onclick = function (){
    refesh();
}

//点击连接时
var a = document.getElementById("refesh");
a.onclick = function (){
    refesh();
    //返回false防止a标签默认href行为
    return false;
}

function refesh() {
    /**
     * 由于路径相同时浏览器会自动调用缓存中的图片
     * 所以在连接后加时间戳解决此问题
     */
    var date = new Date().getTime();
    img.src = "identcode?" + date;
}
  • 效果

在这里插入图片描述
在这里插入图片描述

项目源码

https://github.com/xiaochen0517/StudySpace/tree/master/idea/TestDemo3

  • 29
    点赞
  • 103
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
首先需要引入JavaMail API和Java Activation Framework (JAF)的包。 以下是一个简单的Java代码示例,用于从QQ邮箱发送验证码邮件: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { final String username = "your_qq_email@qq.com"; final String password = "your_email_password"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.qq.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("your_qq_email@qq.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient_email@example.com")); message.setSubject("Verification Code"); int code = 123456; // replace with actual code message.setText("Your verification code is: " + code); Transport.send(message); System.out.println("Code sent successfully!"); } catch (MessagingException e) { throw new RuntimeException(e); } } } ``` 在上面的代码中: - 将`your_qq_email@qq.com`替换为您的QQ邮箱地址。 - 将`your_email_password`替换为您的QQ邮箱密码。 - 将`recipient_email@example.com`替换为收件人的电子邮件地址。 - 将`123456`替换为实际的验证码。 运行代码后,您应该能够在收件人的邮箱中收到一封包含验证码的电子邮件。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌尘吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值