Java io生成验证码及easy-captcha实现验证码功能

£神魔★判官ぃ 2024-03-22 20:17 134阅读 0赞
  1. import javax.imageio.ImageIO;
  2. import java.awt.*;
  3. import java.awt.geom.GeneralPath;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.util.Random;
  7. import java.util.UUID;
  8. public class CheckDemo1 {
  9. public static void main(String[] args) throws Exception {
  10. //for (int i = 0; i < 100; i++) {
  11. check();
  12. //}
  13. }
  14. public static void check() throws Exception {
  15. int w = 160;
  16. int h = 60;
  17. int len = 5;
  18. Random rand = new Random();
  19. BufferedImage i = new BufferedImage(w, h, 2);
  20. Graphics2D g = i.createGraphics();
  21. g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
  22. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  23. g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
  24. g.setColor(new Color(251, 251, 251));
  25. g.fillRect(0, 0, w, h);
  26. Font font = new Font("宋体", Font.BOLD, 15);
  27. font = font.deriveFont(Font.PLAIN, 55f);
  28. g.setFont(font);
  29. String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  30. double radianPercent = 0D;
  31. for (int n = 0; n < 20; n++) {
  32. font = font.deriveFont(Font.PLAIN, rand.nextFloat() * 15 + 15);
  33. g.setFont(font);
  34. g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(10) + 50));
  35. int x = rand.nextInt(w);
  36. int y = rand.nextInt(h);
  37. g.drawString(String.valueOf(str.charAt(rand.nextInt(str.length()))), x, y);
  38. }
  39. StringBuilder sok = new StringBuilder(len);
  40. g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(25) + 220));
  41. for (int n = 0; n < len; n++) {
  42. font = font.deriveFont(Font.PLAIN, rand.nextFloat() * 35 + 30);
  43. g.setFont(font);
  44. int x = n * 32 + 5;
  45. int y = rand.nextInt(35) + 20;
  46. radianPercent = Math.PI * (rand.nextInt(35) / 180D);
  47. if (rand.nextBoolean()) radianPercent = -radianPercent;
  48. g.rotate(radianPercent, x, y);
  49. String sss = String.valueOf(str.charAt(rand.nextInt(str.length())));
  50. sok.append(sss);
  51. g.drawString(sss, x, y);
  52. g.rotate(-radianPercent, x, y);
  53. }
  54. Point[] points = {new Point(0, 0), new Point(10, 50), new Point(30, 6), new Point(60, 55), new Point(80, 3), new Point(160, 60)};
  55. GeneralPath path = new GeneralPath();
  56. path.moveTo(points[0].x, points[0].y);
  57. for (int i2 = 0; i2 < points.length - 1; ++i2) {
  58. Point sp = points[i2];
  59. Point ep = points[i2 + 1];
  60. Point c1 = new Point((sp.x + ep.x) / 2, sp.y);
  61. Point c2 = new Point((sp.x + ep.x) / 2, ep.y);
  62. path.curveTo(c1.x, c1.y, c2.x, c2.y, ep.x, ep.y);
  63. }
  64. g.setStroke(new BasicStroke(rand.nextInt(6) + 2));
  65. g.draw(path);
  66. g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(25) + 220));
  67. int yy = rand.nextInt(15) + 15;// y
  68. int hh = rand.nextInt(20) + 10;// 高度
  69. int aa = rand.nextInt(60) + 20;//
  70. // x
  71. for (int x = 10; x < w; x++) {
  72. int y = (int) (yy + hh * Math.sin(x * Math.PI / aa));
  73. // g.drawLine(x, (int) y, x, (int) y);
  74. g.fillOval(x, y, 3, 3);
  75. }
  76. // 输出验证码
  77. System.out.println(sok);
  78. g.dispose();
  79. ImageIO.write(i, "png", new File("c:/cc/" + UUID.randomUUID() + ".png"));
  80. Runtime run = Runtime.getRuntime();
  81. }
  82. }

7fae6f68751440f08272409f969978ec.png

42a62b0f30f246ef875de19bd1acfc7a.png

easy-captcha

EasyCaptcha 是一个简单易用的 Java 验证码生成工具,它提供了简单的 API 来生成和验证各种类型的验证码。

77c9fd148c4a44f7a9adc0154e1afe4f.png

要使用 EasyCaptcha,首先你需要添加 EasyCaptcha 的依赖到你的项目中。可以通过 Maven 或 Gradle 进行添加。

在 Maven 中,你可以在 pom.xml 文件中添加以下依赖:

  1. <dependency>
  2. <groupId>com.github.qcloudsms</groupId>
  3. <artifactId>easycaptcha</artifactId>
  4. <version>1.5.1</version>
  5. </dependency>

在 Gradle 中,你可以在 build.gradle 文件的 dependencies 部分中添加以下依赖:

  1. implementation 'com.github.qcloudsms:easycaptcha:1.5.1'

一旦你添加了 EasyCaptcha 的依赖,你就可以开始使用它了。

  • png类型

    @RequestMapping(“/hello”)
    public void hello(HttpServletResponse response) throws IOException {

    1. // png类型
    2. SpecCaptcha captcha = new SpecCaptcha(130, 48);
    3. String text = captcha.text();// 获取验证码的字符
    4. char[] chars = captcha.textChar();// 获取验证码的字符数组
    5. System.out.println("验证码:"+text);
    6. System.out.println(chars);
    7. // 输出验证码
    8. captcha.out(response.getOutputStream());

    }

20200502174109279.png

  • gif类型

    @RequestMapping(“/hello”)
    public void hello(HttpServletResponse response) throws IOException {

    1. // 三个参数分别为宽、高、位数
    2. GifCaptcha gifCaptcha = new GifCaptcha(100, 48, 4);
    3. // 设置类型:字母数字混合
    4. gifCaptcha.setCharType(Captcha.TYPE_DEFAULT);
    5. //获取验证码
    6. String text = gifCaptcha.text();
    7. System.out.println("验证码为:"+text);
    8. // 输出验证码
    9. gifCaptcha.out(response.getOutputStream());

    }

aHR0cHM6Ly9zMi5heDF4LmNvbS8yMDE5LzA4LzIzL21zRnpWSy5naWY

  • 中文类型

    @RequestMapping(“/hello”)
    public void hello(HttpServletResponse response) throws IOException {

    1. // 中文类型
    2. ChineseCaptcha captcha = new ChineseCaptcha(130, 48);
    3. //获取验证码
    4. String text = captcha.text();
    5. System.out.println("验证码为:"+text);
    6. // 输出验证码
    7. captcha.out(response.getOutputStream());

    }

20200502174920911.png

  • 算术类型

    @RequestMapping(“/hello”)
    public void hello(HttpServletResponse response) throws IOException {

    1. // 算术类型
    2. ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
    3. captcha.setLen(3); // 几位数运算,默认是两位
    4. captcha.getArithmeticString(); // 获取运算的公式:4-9+1=?
    5. String text = captcha.text();// 获取运算的结果:-4
    6. System.out.println("计算结果为:"+text);
    7. // 输出验证码
    8. captcha.out(response.getOutputStream());

    }

在这里插入图片描述

用户输入验证码后,你可以通过比较用户输入的验证码和生成的验证码的值来验证其正确性。

EasyCaptcha 还提供了一些其他的配置选项,例如自定义验证码的宽度、高度、字体、干扰线等等。

注意:请确保在使用验证码时遵循适当的安全措施,以防止滥用、暴力破解或其他恶意行为。

发表评论

表情:
评论列表 (有 0 条评论,134人围观)

还没有评论,来说两句吧...

相关阅读

    相关 javaJava验证功能实现

    一、前言       验证码可以说在我们生活中已经非常普遍了,任何一个网站,任何一个App都会有这个功能,但是为啥要有这个呢?如何做才能做出来呢?下面小编会带领大家一起用