Spring Boot2 系列教程(三十四)Spring Security 添加验证码

g2.setColor(bgColor);// 设置验证码图片的背景颜色

g2.fillRect(0, 0, width, height);

return image;

}

public BufferedImage getImage() {

BufferedImage image = createImage();

Graphics2D g2 = (Graphics2D) image.getGraphics();

StringBuffer sb = new StringBuffer();

for (int i = 0; i < 4; i++) {

String s = randomChar() + “”;

sb.append(s);

g2.setColor(randomColor());

g2.setFont(randomFont());

float x = i * width * 1.0f / 4;

g2.drawString(s, x, height - 15);

}

this.text = sb.toString();

drawLine(image);

return image;

}

/**

  • 绘制干扰线

  • @param image

*/

private void drawLine(BufferedImage image) {

Graphics2D g2 = (Graphics2D) image.getGraphics();

int num = 5;

for (int i = 0; i < num; i++) {

int x1 = random.nextInt(width);

int y1 = random.nextInt(height);

int x2 = random.nextInt(width);

int y2 = random.nextInt(height);

g2.setColor(randomColor());

g2.setStroke(new BasicStroke(1.5f));

g2.drawLine(x1, y1, x2, y2);

}

}

public String getText() {

return text;

}

public static void output(BufferedImage image, OutputStream out) throws IOException {

ImageIO.write(image, “JPEG”, out);

}

}

这个工具类很常见,网上也有很多,就是画一个简单

  • 20
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值