使用base64实现验证码功能

野性酷女 2024-04-17 05:33 76阅读 0赞

本代码摘自网络,因时间久了忘记位置,无法表明出处,侵删。

1.验证码类

  1. import java.io.Serializable;
  2. /** * * @ClassName: Validate * @Description: 验证码类 * @author chenhx * @date 2017年11月14日 上午11:35:34 */
  3. public class Validate implements Serializable{
  4. private static final long serialVersionUID = 1L;
  5. private String Base64Str; //Base64 值
  6. private String value; //验证码值
  7. public String getBase64Str() {
  8. return Base64Str;
  9. }
  10. public void setBase64Str(String base64Str) {
  11. Base64Str = base64Str;
  12. }
  13. public String getValue() {
  14. return value;
  15. }
  16. public void setValue(String value) {
  17. this.value = value;
  18. }
  19. }

2.base64生成工具类

  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.image.BufferedImage;
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.IOException;
  8. import java.util.Random;
  9. import javax.imageio.ImageIO;
  10. import org.apache.commons.lang.math.RandomUtils;
  11. import com.alibaba.druid.util.Base64;
  12. public class ValidateCodeUtil {
  13. private static Validate validate = null; //验证码类,用于最后返回此对象,包含验证码图片base64和真值
  14. private static Random random = new Random(); //随机类,用于生成随机参数
  15. private static String randString = "0123456789abcdefghijkmnpqrtyABCDEFGHIJLMNQRTY";//随机生成字符串的取值范围
  16. private static int width = 80; //图片宽度
  17. private static int height = 34; //图片高度
  18. private static int StringNum = 4; //字符的数量
  19. private static int lineSize = 40; //干扰线数量
  20. //将构造函数私有化 禁止new创建
  21. private ValidateCodeUtil() {
  22. super();
  23. }
  24. /** * 获取随机字符,并返回字符的String格式 * @param index (指定位置) * @return */
  25. private static String getRandomChar(int index) {
  26. //获取指定位置index的字符,并转换成字符串表示形式
  27. return String.valueOf(randString.charAt(index));
  28. }
  29. /** * 获取随机指定区间的随机数 * @param min (指定最小数) * @param max (指定最大数) * @return */
  30. private static int getRandomNum(int min,int max) {
  31. return RandomUtils.nextInt(min);
  32. // return RandomUtils.nextInt(min, max);
  33. }
  34. /** * 获得字体 * @return */
  35. private static Font getFont() {
  36. return new Font("Fixedsys", Font.CENTER_BASELINE, 25); //名称、样式、磅值
  37. }
  38. /** * 获得颜色 * @param fc * @param bc * @return */
  39. private static Color getRandColor(int frontColor, int backColor) {
  40. if(frontColor > 255)
  41. frontColor = 255;
  42. if(backColor > 255)
  43. backColor = 255;
  44. int red = frontColor + random.nextInt(backColor - frontColor - 16);
  45. int green = frontColor + random.nextInt(backColor - frontColor -14);
  46. int blue = frontColor + random.nextInt(backColor - frontColor -18);
  47. return new Color(red, green, blue);
  48. }
  49. /** * 绘制字符串,返回绘制的字符串 * @param g * @param randomString * @param i * @return */
  50. private static String drawString(Graphics g, String randomString, int i) {
  51. Graphics2D g2d = (Graphics2D) g;
  52. g2d.setFont(getFont()); //设置字体
  53. g2d.setColor(new Color(random.nextFloat(), random.nextFloat(), random.nextFloat()));//设置颜色
  54. String randChar = String.valueOf(getRandomChar(random.nextInt(randString.length())));
  55. randomString += randChar; //组装
  56. int rot = getRandomNum(5,10);
  57. g2d.translate(random.nextInt(3), random.nextInt(3));
  58. g2d.rotate(rot * Math.PI / 180);
  59. g2d.drawString(randChar, 13*i, 20);
  60. g2d.rotate(-rot * Math.PI / 180);
  61. return randomString;
  62. }
  63. /** * 绘制干扰线 * @param g */
  64. private static void drawLine(Graphics g) {
  65. //起点(x,y) 偏移量x1、y1
  66. int x = random.nextInt(width);
  67. int y = random.nextInt(height);
  68. int xl = random.nextInt(13);
  69. int yl = random.nextInt(15);
  70. g.setColor(new Color(random.nextFloat(), random.nextFloat(), random.nextFloat()));
  71. g.drawLine(x, y, x + xl, y + yl);
  72. }
  73. /** * * @MethodName: getRandomCode * @Description: 生成Base64图片验证码 * @param key * @return String 返回base64 */
  74. public static Validate getRandomCode() {
  75. validate = validate==null?new Validate():validate;
  76. // BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
  77. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
  78. Graphics g = image.getGraphics();// 获得BufferedImage对象的Graphics对象
  79. g.fillRect(0, 0, width, height);//填充矩形
  80. g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));//设置字体
  81. g.setColor(getRandColor(110, 133));//设置颜色
  82. //绘制干扰线
  83. for(int i = 0; i <= lineSize; i++) {
  84. drawLine(g);
  85. }
  86. //绘制字符
  87. String randomString = "";
  88. for(int i = 1; i <= StringNum; i++) {
  89. randomString = drawString(g, randomString, i);
  90. validate.setValue(randomString);
  91. }
  92. g.dispose();//释放绘图资源
  93. ByteArrayOutputStream bs = null;
  94. try {
  95. bs = new ByteArrayOutputStream();
  96. ImageIO.write(image, "png", bs);//将绘制得图片输出到流
  97. String imgsrc = Base64.byteArrayToBase64(bs.toByteArray());
  98. validate.setBase64Str(imgsrc);
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. } finally {
  102. try {
  103. bs.close();
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }finally{
  107. bs = null;
  108. }
  109. }
  110. return validate;
  111. }
  112. }

3. 测试调用

  1. public class Test {
  2. public static void main(String[] args) {
  3. Validate v = ValidateCodeUtil.getRandomCode();
  4. System.out.println(v.getBase64Str());
  5. System.out.println(v.getValue());
  6. }
  7. }

发表评论

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

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

相关阅读

    相关 javaweb实现验证功能

    在javaweb的用户注册与登陆功能时,有时为了防止漏洞或者大量注册,可以使用验证码功能,下面是验证码的一个简单实现 验证码类 public class V

    相关 邮箱验证功能实现

    在进行实际项目的开发过程中,我们可能会遇到需要需要对用户账号密码进行找回,这时就需要我们通过邮件进行验证,从而对用户信息进行修改,下面我就将自己开发过程中的邮件发送功能简单描述