All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hecloud.runtime.common.utils.Captcha Maven / Gradle / Ivy

package com.hecloud.runtime.common.utils;

import lombok.Data;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.Serializable;
import java.util.Random;

/**
 * 验证码生成器 ,一个单例模式的工具类,使用Captcha.instance()方法获取Captcha的对象实例;
 * 利用实例的genCode(codeCount)获取生成的验证码以及Image对象;
 * codeCount表示验证码的数量,最大为6位;最小为1位。
 *
 * @author LoveinBJ
 */
@Data
public class Captcha implements Serializable {
    private static final long serialVersionUID = 1L;
    static char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
            'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
            'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '=', '-', '~'};
    /**
     * 定义图片的width
     */
    private static int width = 130;
    /**
     * 定义图片的height
     */
    private static int height = 30;
    private static int xx = 20;
    private static int fontHeight = 22;
    private static int codeY = 22;
    private static Captcha instance;
    private BufferedImage image;

    private Captcha() {
        if (null != instance) {
            throw new RuntimeException("验证码工具类Captcha正在遭受反射攻击!");
        }
        // 定义图像buffer
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    }

    public static Captcha instance() {
        return CaptchaContainer.instance;
    }

    /**
     * 生成验证码,最大为6位,最小为1位。
     * 执行此方法之后利用Captcha实例的getImage()方法获取验证码图片对象。
     *
     * @param codeCount 验证码数量
     * @return 验证码字符串
     */
    public String genCode(int codeCount) {
        if (codeCount > 32) {
            codeCount = 32;
        } else if (codeCount <= 0) {
            codeCount = 1;
        }
        Graphics graphics = image.getGraphics();
        // 创建一个随机数生成器类
        Random random = new Random();
        // 将图像填充为白色
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, width, height);

        // 创建字体,字体的大小应该根据图片的高度来定。
        Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
        // 设置字体。
        graphics.setFont(font);

        // 画边框。
        graphics.setColor(Color.WHITE);
        graphics.drawRect(0, 0, width - 1, height - 1);

        // 随机产生40条干扰线,使图象中的认证码不易被其它程序探测到。
        graphics.setColor(Color.BLACK);
        for (int i = 0; i < 40; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            graphics.drawLine(x, y, x + xl, y + yl);
        }

        // randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
        StringBuffer randomCode = new StringBuffer();
        int red, green, blue;

        // 随机产生codeCount数字的验证码。
        for (int i = 0; i < codeCount; i++) {
            // 得到随机产生的验证码数字。
            String code = String.valueOf(codeSequence[random.nextInt(36)]);
            // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
            red = random.nextInt(255);
            green = random.nextInt(255);
            blue = random.nextInt(255);
            // 用随机产生的颜色将验证码绘制到图像中。
            graphics.setColor(new Color(red, green, blue));
            graphics.drawString(code, i * xx + 10, codeY);

            // 将产生的四个随机数组合在一起。
            randomCode.append(code);
        }
        return randomCode.toString();
    }

    /**
     * 防止反序列化获取多个对象的漏洞
     *
     * @return 对象
     */
    private Object readResolve() {
        return CaptchaContainer.instance;
    }

    private static class CaptchaContainer {
        private static Captcha instance = new Captcha();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy