科技速递|SpringBoot-登录验证码实现

嗯 , 工具类记录
原链接: , 非常感谢原链接博主
科技速递|SpringBoot-登录验证码实现
科技速递|SpringBoot-登录验证码实现此验证码的实现没有用到太多的插件 , 话不多说直接上代码 , 大家拿过去就可以用 。
1.验证码类
package com.youyou.login.util.validatecode;import lombok.Data;/** * 验证码类 */@Datapublic class VerifyCode {private String code;private byte[] imgBytes;private long expireTime;}
科技速递|SpringBoot-登录验证码实现2.验证码生成接口
package com.youyou.login.util.validatecode;import java.io.IOException;import java.io.OutputStream;/** * 验证码生成接口 */public interface IVerifyCodeGen {/*** 生成验证码并返回code , 将图片写的os中** @param width* @param height* @param os* @return* @throws IOException*/String generate(int width, int height, OutputStream os) throws IOException;/*** 生成验证码对象** @param width* @param height* @return* @throws IOException*/VerifyCode generate(int width, int height) throws IOException;}
科技速递|SpringBoot-登录验证码实现3.验证码生成实现类
package com.youyou.login.util.validatecode;import com.youyou.util.RandomUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Random;/** * 验证码实现类 */public class SimpleCharVerifyCodeGenImpl implements IVerifyCodeGen {private static final Logger logger = LoggerFactory.getLogger(SimpleCharVerifyCodeGenImpl.class);private static final String[] FONT_TYPES = { "\u5b8b\u4f53", "\u65b0\u5b8b\u4f53", "\u9ed1\u4f53", "\u6977\u4f53", "\u96b6\u4e66" };private static final int VALICATE_CODE_LENGTH = 4;/*** 设置背景颜色及大小 , 干扰线** @param graphics* @param width* @param height*/private static void fillBackground(Graphics graphics, int width, int height) {// 填充背景graphics.setColor(Color.WHITE);//设置矩形坐标x y 为0graphics.fillRect(0, 0, width, height);// 加入干扰线条for (int i = 0; i < 8; i++) {//设置随机颜色算法参数graphics.setColor(RandomUtils.randomColor(40, 150));Random random = new Random();int x = random.nextInt(width);int y = random.nextInt(height);int x1 = random.nextInt(width);int y1 = random.nextInt(height);graphics.drawLine(x, y, x1, y1);}}/*** 生成随机字符** @param width* @param height* @param os* @return* @throws IOException*/@Overridepublic String generate(int width, int height, OutputStream os) throws IOException {BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics graphics = image.getGraphics();fillBackground(graphics, width, height);String randomStr = RandomUtils.randomString(VALICATE_CODE_LENGTH);createCharacter(graphics, randomStr);graphics.dispose();//设置JPEG格式ImageIO.write(image, "JPEG", os);return randomStr;}/*** 验证码生成** @param width* @param height* @return*/@Overridepublic VerifyCode generate(int width, int height) {VerifyCode verifyCode = null;try (//将流的初始化放到这里就不需要手动关闭流ByteArrayOutputStream baos = new ByteArrayOutputStream();) {String code = generate(width, height, baos);verifyCode = new VerifyCode();verifyCode.setCode(code);verifyCode.setImgBytes(baos.toByteArray());} catch (IOException e) {logger.error(e.getMessage(), e);verifyCode = null;}return verifyCode;}/*** 设置字符颜色大小** @param g* @param randomStr*/private void createCharacter(Graphics g, String randomStr) {char[] charArray = randomStr.toCharArray();for (int i = 0; i


推荐阅读