5【hutool】hutool-captcha

456 阅读2分钟

该系列文章主要是对 hutool 工具类的介绍,详情可以参考

hutool.cn/docs/#/

该模块主要封装了各类验证码生成及校验功能

核心接口为 ICaptcha

public interface ICaptcha extends Serializable{

	/**
	 * 创建验证码,实现类需同时生成随机验证码字符串和验证码图片
	 */
	void createCode();

	/**
	 * 获取验证码的文字内容
	 *
	 * @return 验证码文字内容
	 */
	String getCode();

	/**
	 * 验证验证码是否正确,建议忽略大小写
	 *
	 * @param userInputCode 用户输入的验证码
	 * @return 是否与生成的一直
	 */
	boolean verify(String userInputCode);

	/**
	 * 将验证码写出到目标流中
	 *
	 * @param out 目标流
	 */
	void write(OutputStream out);
}
  • createCode 创建验证码,实现类需同时生成随机验证码字符串和验证码图片
  • getCode 获取验证码的文字内容
  • verify 验证验证码是否正确,建议忽略大小写
  • write 将验证码写出到目标流中
@Test
public void lineCaptchaTest1() {
    // 定义图形验证码的长和宽
    LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
    Console.log(lineCaptcha.getCode());
    Console.log(lineCaptcha.verify(lineCaptcha.getCode()));
}

@Test
public void lineCaptchaTest2() {
    // 定义图形验证码的长和宽
    LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
    // 图形验证码写出,可以写出到文件,也可以写出到流
    lineCaptcha.write("C:\\Users\\fanzh23\\Pictures\\captcha\\line.png");
    Console.log(lineCaptcha.getCode());
    // 验证图形验证码的有效性,返回boolean值
    Console.log(lineCaptcha.verify("1234"));
}

@Test
public void lineCaptchaTest3() {
    // 定义图形验证码的长和宽
    LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 70, 4, 15);
    lineCaptcha.setBackground(Color.YELLOW);
    lineCaptcha.write("C:\\Users\\fanzh23\\Pictures\\captcha\\line-bg.png");
    Console.log(lineCaptcha.getCode());
    // 验证图形验证码的有效性,返回boolean值
    Console.log(lineCaptcha.verify("1234"));
}

@Test
public void lineCaptchaWithMathTest() {
    // 定义图形验证码的长和宽
    LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 80);
    lineCaptcha.setGenerator(new MathGenerator());
    lineCaptcha.setTextAlpha(0.8f);
    lineCaptcha.write("C:\\Users\\fanzh23\\Pictures\\captcha\\line-math.png");
    Console.log(lineCaptcha.getCode());
    // 计算
    Console.log(Calculator.conversion(lineCaptcha.getCode()));
}


@Test
public void circleCaptchaTest() {
    // 定义图形验证码的长和宽
    CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
    // 图形验证码写出,可以写出到文件,也可以写出到流
    captcha.write("C:\\Users\\fanzh23\\Pictures\\captcha\\circle.png");
    Console.log(captcha.getCode());
    // 验证图形验证码的有效性,返回boolean值
    Console.log(captcha.verify("1234"));
}

@Test
public void shearCaptchaTest() {
    // 定义图形验证码的长和宽
    ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(203, 100, 4, 4);
    // 图形验证码写出,可以写出到文件,也可以写出到流
    captcha.write("C:\\Users\\fanzh23\\Pictures\\captcha\\shear.png");
    Console.log(captcha.getCode());
    // 验证图形验证码的有效性,返回boolean值
    Console.log(captcha.verify("1234"));
}

@Test
public void gifCaptchaTest() {
    GifCaptcha captcha = CaptchaUtil.createGifCaptcha(200, 100, 4);
    captcha.write("C:\\Users\\fanzh23\\Pictures\\captcha\\gif_captcha.gif");
    Console.log(captcha.getCode());
    // 验证图形验证码的有效性,返回boolean值
    Console.log(captcha.verify("1234"));
}