前言

项目需要使用登录验证码功能(以前写过但是具体怎么实现的忘记了,现在记录一下)
网上找到的博客大多都是默认的实现验证码,后期自己加工了一下实现自定义的功能

实现效果图。借鉴网上的优秀文章,仅供个人记录学习

实现代码如下

第一步
创建KaptchaConfig配置类

@Configuration
public class KaptchaConfig {@Beanpublic DefaultKaptcha getDefaultKaptcha(){DefaultKaptcha captchaProducer = new DefaultKaptcha();Properties properties = new Properties();properties.setProperty("kaptcha.border", "no");properties.setProperty("kaptcha.image.width", "400");properties.setProperty("kaptcha.image.height", "400");properties.setProperty("kaptcha.session.key", "code");//文字渲染器properties.setProperty("kaptcha.word.impl", "xxxx包路径名.WordIml");//文本实现类properties.setProperty("kaptcha.textproducer.impl", "xxxx包路径名.TextIml");//背景颜色实现类properties.setProperty("kaptcha.background.impl", "xxxx包路径名.BackgroundKaptcha");//图片样式实现类properties.setProperty("kaptcha.obscurificator.impl","xxxx包路径名.NoWaterRipple");Config config = new Config(properties);captchaProducer.setConfig(config);return captchaProducer;}
}

第二步
创建背景颜色实现类

/*** @Classname BackgroundKaptcha  验证码背景颜色实现类* @Description* @Date 2022-02-14 10:16:59* @Created by juanita.ren*/
//@Configuration
public class BackgroundKaptcha extends Configurable implements BackgroundProducer {@Overridepublic BufferedImage addBackground(BufferedImage baseImage) {int width = baseImage.getWidth();int height = baseImage.getHeight();BufferedImage imageWithBackground = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics2D graph = (Graphics2D) imageWithBackground.getGraphics();//此处的new color()里面的参数可以改成自己需要的背景颜色,暂不支持透明度,可能是我的版本有问题,透明度没有实现成功graph.drawImage(baseImage, 0, 0,new Color(17,28,100),null);return imageWithBackground;}
}

第三步
创建干扰线条实现类

/*** @Classname NoWaterRipple  干扰线工具类* @Description* @Date 2022-02-11 10:04:37* @Created by juanita.ren*/
public class NoWaterRipple extends Configurable implements GimpyEngine {public static final int WIDTH = 400;//生成的图片的宽度public static final int HEIGHT = 125;//生成的图片的高度public static Font font[] = {new Font("宋体", Font.BOLD, 24),new Font("宋体", Font.BOLD, 24)};public NoWaterRipple(){}@Overridepublic BufferedImage getDistortedImage(BufferedImage baseImage) {BufferedImage distortedImage = new BufferedImage(baseImage.getWidth(), baseImage.getHeight(), 2);Graphics2D graphics = (Graphics2D)distortedImage.getGraphics();graphics.drawImage(baseImage, 0, 0, (Color)null, (ImageObserver)null);//绘制干扰线drawRandomLine(graphics);graphics.dispose();return distortedImage;}/*** 设置随机干扰线条* @param g*/public static void drawRandomLine(Graphics g) {// 设置线条个数并画线for (int i = 0; i < 1; i++) {// 设置颜色g.setColor(getRandColorCode());int x1 = new Random().nextInt(WIDTH / 2);int y1 = new Random().nextInt(HEIGHT / 2);int x2 = new Random().nextInt(WIDTH) + WIDTH / 2;int y2 = new Random().nextInt(HEIGHT) + HEIGHT / 2;Graphics2D g2 = (Graphics2D) g;// 设置笔画的属性,设置线条的粗细g2.setStroke(new BasicStroke(10f));g2.drawLine(x1, y1, x2, y2);}}/*** 随机颜色* @return*/public static Color getRandColorCode() {int r, g, b;Random random = new Random();r = random.nextInt(130)+125;g = random.nextInt(130)+125;b = random.nextInt(256);Color color = new Color(r, g, b);return color;}
}

第四步
创建字体样式实现类

/*** @Classname TextProduceIml  验证码字体样式工具类* @Description* @Date 2022-02-14 16:43:00* @Created by juanita.ren*/
//@Configuration
public class WordIml extends Configurable implements WordRenderer {public WordIml(){}/*** 设置字体*/public static Font font[] = {new Font("宋体", Font.BOLD, 120),new Font("宋体", Font.BOLD, 120)};/*** @param verify调用String capText = Producer.createText();生成好的* @param baseChar* @return 随机字符*/public static String createRandomChar(Graphics2D g, String baseChar, String verify) {StringBuffer stringBuffer = new StringBuffer();//此处的x可以控制验证码左右字体移动int x = 50;String ch = "";// 设置生成字数for (int i = 0; i < 4; i++) {g.setFont(font[new Random().nextInt(font.length - 1)]);g.setColor(getRandColorCode());// 设置字体旋转角度int degree = new Random().nextInt() % 30;//原来文章上看的是这样的,随机生成一个字符串,后面根据自己的要求改成了verify.substring(i, i + 1))//ch = baseChar.charAt(new Random().nextInt(baseChar.length())) + "";ch = baseChar.charAt(Integer.valueOf(verify.substring(i, i + 1))) + "";stringBuffer.append(ch);// 正向角度g.rotate(degree * Math.PI / 180, x, 80);g.drawString(ch, x, 120);// 反向角度//此处的80可以控制验证码字体的上下移动g.rotate(-degree * Math.PI / 180, x, 80);x += 80;}return stringBuffer.toString();}/*** 随机颜色* @return*/public static Color getRandColorCode() {int r, g, b;Random random = new Random();//设置生成的颜色都是浅色调范围r = random.nextInt(130)+125;g = random.nextInt(130)+125;b = random.nextInt(256);Color color = new Color(r, g, b);return color;}/*** 画随机字符* @param g* @param createTypeFlag* @return String*/public static String drawRandomNum(Graphics2D g, String createTypeFlag, String verify) {// 设置颜色g.setColor(Color.blue);// 设置字体g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 120));// 常用的中国汉字String baseChineseChar = "\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u7740\u4e2a\u5730\u5230\u5927\u91cc\u8bf4\u5c31\u53bb\u5b50\u5f97\u4e5f\u548c\u90a3\u8981\u4e0b\u770b\u5929\u65f6\u8fc7\u51fa\u5c0f\u4e48\u8d77\u4f60\u90fd\u628a\u597d\u8fd8\u591a\u6ca1\u4e3a\u53c8\u53ef\u5bb6\u5b66\u53ea\u4ee5\u4e3b\u4f1a\u6837\u5e74\u60f3\u751f\u540c\u8001\u4e2d\u5341\u4ece\u81ea\u9762\u524d\u5934\u9053\u5b83\u540e\u7136\u8d70\u5f88\u50cf\u89c1\u4e24\u7528\u5979\u56fd\u52a8\u8fdb\u6210\u56de\u4ec0\u8fb9\u4f5c\u5bf9\u5f00\u800c\u5df1\u4e9b\u73b0\u5c71\u6c11\u5019\u7ecf\u53d1\u5de5\u5411\u4e8b\u547d\u7ed9\u957f\u6c34\u51e0\u4e49\u4e09\u58f0\u4e8e\u9ad8\u624b\u77e5\u7406\u773c\u5fd7\u70b9\u5fc3\u6218\u4e8c\u95ee\u4f46\u8eab\u65b9\u5b9e\u5403\u505a\u53eb\u5f53\u4f4f\u542c\u9769\u6253\u5462\u771f\u5168\u624d\u56db\u5df2\u6240\u654c\u4e4b\u6700\u5149\u4ea7\u60c5\u8def\u5206\u603b\u6761\u767d\u8bdd\u4e1c\u5e2d\u6b21\u4eb2\u5982\u88ab\u82b1\u53e3\u653e\u513f\u5e38\u6c14\u4e94\u7b2c\u4f7f\u5199\u519b\u5427\u6587\u8fd0\u518d\u679c\u600e\u5b9a\u8bb8\u5feb\u660e\u884c\u56e0\u522b\u98de\u5916\u6811\u7269\u6d3b\u90e8\u95e8\u65e0\u5f80\u8239\u671b\u65b0\u5e26\u961f\u5148\u529b\u5b8c\u5374\u7ad9\u4ee3\u5458\u673a\u66f4\u4e5d\u60a8\u6bcf\u98ce\u7ea7\u8ddf\u7b11\u554a\u5b69\u4e07\u5c11\u76f4\u610f\u591c\u6bd4\u9636\u8fde\u8f66\u91cd\u4fbf\u6597\u9a6c\u54ea\u5316\u592a\u6307\u53d8\u793e\u4f3c\u58eb\u8005\u5e72\u77f3\u6ee1\u65e5\u51b3\u767e\u539f\u62ff\u7fa4\u7a76\u5404\u516d\u672c\u601d\u89e3\u7acb\u6cb3\u6751\u516b\u96be\u65e9\u8bba\u5417\u6839\u5171\u8ba9\u76f8\u7814\u4eca\u5176\u4e66\u5750\u63a5\u5e94\u5173\u4fe1\u89c9\u6b65\u53cd\u5904\u8bb0\u5c06\u5343\u627e\u4e89\u9886\u6216\u5e08\u7ed3\u5757\u8dd1\u8c01\u8349\u8d8a\u5b57\u52a0\u811a\u7d27\u7231\u7b49\u4e60\u9635\u6015\u6708\u9752\u534a\u706b\u6cd5\u9898\u5efa\u8d76\u4f4d\u5531\u6d77\u4e03\u5973\u4efb\u4ef6\u611f\u51c6\u5f20\u56e2\u5c4b\u79bb\u8272\u8138\u7247\u79d1\u5012\u775b\u5229\u4e16\u521a\u4e14\u7531\u9001\u5207\u661f\u5bfc\u665a\u8868\u591f\u6574\u8ba4\u54cd\u96ea\u6d41\u672a\u573a\u8be5\u5e76\u5e95\u6df1\u523b\u5e73\u4f1f\u5fd9\u63d0\u786e\u8fd1\u4eae\u8f7b\u8bb2\u519c\u53e4\u9ed1\u544a\u754c\u62c9\u540d\u5440\u571f\u6e05\u9633\u7167\u529e\u53f2\u6539\u5386\u8f6c\u753b\u9020\u5634\u6b64\u6cbb\u5317\u5fc5\u670d\u96e8\u7a7f\u5185\u8bc6\u9a8c\u4f20\u4e1a\u83dc\u722c\u7761\u5174\u5f62\u91cf\u54b1\u89c2\u82e6\u4f53\u4f17\u901a\u51b2\u5408\u7834\u53cb\u5ea6\u672f\u996d\u516c\u65c1\u623f\u6781\u5357\u67aa\u8bfb\u6c99\u5c81\u7ebf\u91ce\u575a\u7a7a\u6536\u7b97\u81f3\u653f\u57ce\u52b3\u843d\u94b1\u7279\u56f4\u5f1f\u80dc\u6559\u70ed\u5c55\u5305\u6b4c\u7c7b\u6e10\u5f3a\u6570\u4e61\u547c\u6027\u97f3\u7b54\u54e5\u9645\u65e7\u795e\u5ea7\u7ae0\u5e2e\u5566\u53d7\u7cfb\u4ee4\u8df3\u975e\u4f55\u725b\u53d6\u5165\u5cb8\u6562\u6389\u5ffd\u79cd\u88c5\u9876\u6025\u6797\u505c\u606f\u53e5\u533a\u8863\u822c\u62a5\u53f6\u538b\u6162\u53d4\u80cc\u7ec6";//数字和字母的组合String baseNumLetter = "0123456789ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";//纯数字String baseNum = "0123456789";//纯字母String baseLetter = "ABCDEFGHJKLMNOPQRSTUVWXYZ";/*if (createTypeFlag.length > 0 && null != createTypeFlag[0]) {if (createTypeFlag[0].equals("ch")) {// 截取汉字return createRandomChar(g, baseChineseChar);} else if (createTypeFlag[0].equals("nl")) {// 截取数字和字母的组合return createRandomChar(g, baseNumLetter);} else if (createTypeFlag[0].equals("n")) {// 截取数字return createRandomChar(g, baseNum);} else if (createTypeFlag[0].equals("l")) {// 截取字母return createRandomChar(g, baseLetter);}} else {// 默认截取数字和字母的组合return createRandomChar(g, baseNumLetter);}*/createRandomChar(g,baseNum,verify);return "";}//这里传参的 s就是生成的验证码数字,一开始没弄懂renderWord这个怎么用的,出现了问题@Overridepublic BufferedImage renderWord(String s, int i, int i1) {BufferedImage distortedImage = new BufferedImage(400, 160, 2);Graphics2D graphics = (Graphics2D)distortedImage.getGraphics();//设置验证码字体样式String capText = drawRandomNum(graphics, "n", s);return distortedImage;}
}

第五步
验证码生辰controller

/*** @Classname VerificationCodeController 生成验证码功能* @Description* @Date 2022-02-10 11:02:54* @Created by juanita.ren*/
@Api(tags = "验证码")
@RestController
@RequestMapping("/VerificationCode")
public class VerificationCodeController {@Autowiredprivate Producer captchaProducer = null;@Autowiredprivate RedisTemplate<String, String> redisTemplate;@GetMapping("/getVerifiCode")@ResponseBody@ApiOperation(notes = "验证码生成", value = "验证码生成")@ApiImplicitParams({@ApiImplicitParam(value = "验证码唯一键", name = "uuid", dataType = "string", required = true),})public void getVerifiCode( HttpServletResponse response, String uuid) throws IOException {if (StringUtils.isBlank(uuid)){return;}//防止浏览器缓存response.setDateHeader("Expires", 0);response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");response.addHeader("Cache-Control", "post-check=0, pre-check=0");response.setHeader("Pragma", "no-cache");response.setContentType("image/jpeg");//生成验证码String capText = captchaProducer.createText();//设置redis过期时间时1分钟redisTemplate.opsForValue().set(uuid, capText,60, TimeUnit.SECONDS);//向客户端写出图片流BufferedImage bi = captchaProducer.createImage(capText);ServletOutputStream out = response.getOutputStream();ImageIO.write(bi, "jpg", out);try {out.flush();} finally {out.close();}}}

Kaptcha:验证码生成相关推荐

  1. 验证码生成工具Kaptcha

    验证码的作用 防止恶意破解密码.刷票.论坛灌水.刷页. 有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试,实际上使用验证码是现在很多网站通行的方式(比如招商银行的网上个人银 ...

  2. Spring MVC 中使用 Google kaptcha 验证码

    验证码是抵抗批量操作和恶意登录最有效的方式之一. 验证码从产生到现在已经衍生出了很多分支.方式.google kaptcha 是一个非常实用的验证码生成类库. 通过灵活的配置生成各种样式的验证码,并将 ...

  3. kaptcha验证码组件使用简介

    kaptcha验证码组件使用简介 Kaptcha是一个基于SimpleCaptcha的验证码开源项目. 官网地址:http://code.google.com/p/kaptcha/ kaptcha的使 ...

  4. kaptcha 验证码在spring mvc 中的使用

    转自:http://ttaale.iteye.com/blog/808719 kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的 ...

  5. web页面 验证码 生成

    web页面 验证码 生成 kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kapt ...

  6. Spring Boot 整合 Shiro(三)Kaptcha验证码 附源码

    前言 本文是根据上篇<Spring Boot 整合Shiro(二)加密登录与密码加盐处理>进行修改,如有不明白的转上篇文章了解. 1.导入依赖 <!-- https://mvnrep ...

  7. Kaptcha验证码实现

    文章目录 实现流程 用户认证 实现流程 1.先引用了google的验证码生成器(Kaptcha) <dependency><groupId>com.github.axet< ...

  8. Java Kaptcha验证码

    Kaptcha 简介 Kaptcha 是一个可高度配置的实用验证码生成工具,可自由配置的选项如: 验证码的字体 验证码字体的大小 验证码字体的字体颜色 验证码内容的范围(数字,字母,中文汉字!) 验证 ...

  9. Springboot整合kaptcha验证码

    Springboot整合kaptcha验证码 01.通过配置类来配置kaptcha 01-01.添加kaptcha的依赖: <!-- kaptcha验证码 --> <dependen ...

最新文章

  1. 计算机二级7月考试,2020年计算机二级MS Office考试每日一练(7月27日)
  2. JSP简单练习-猜字母游戏
  3. sparkmllib scala GBDT Demo
  4. 如何处理HTML5新标签的浏览器兼容问题?
  5. linux deepin 升级内核命令
  6. ubuntu下搭建nfs服务器
  7. 窗口位置按钮取消_VBA002:“宏”的保存位置有哪几种方式?
  8. 深度好文 | 战“疫”上云正当时:打开云计算的正确姿势
  9. 华为鸿蒙系统还没发布吗,华为没有孤军奋战,合作伙伴“雪中送炭”,鸿蒙系统正式发布!...
  10. 【OpenCV 例程200篇】96. 谐波平均滤波器
  11. 飞鸽传书网站最新改版
  12. 各大公司应聘电子类题目精选
  13. SPEC-RFC3261总述
  14. mui 点击长按复制文本
  15. 企业云计算运营模式,主要分为哪3种运营模式?
  16. b250支持服务器cpu,b250m主板上什么cpu
  17. C#进程间通信的几种方式:管道通信
  18. 中国百货业重磅报告!新零售玩得好的已经尝到甜头了
  19. LeaRun快速开发平台,快速开发.net/java项目
  20. 用windows系统下的DOS命令将腾讯视频客户端下载的qlv文件转换成MP4格式(图文详解)

热门文章

  1. Matlab中readmatrix用法
  2. R | 可视化 | 热图(Heatmap)
  3. 深入讨论DECLARE_HANDLE(HINSTANCE)
  4. Swift 5 Type Metadata 详解
  5. No space left on device: mod_rewrite: could not create rewrite_log_lock
  6. 行业便携终端串口扩展方案
  7. 【Linux】新唐NUC977系统编译及烧写流程
  8. 软件测试职业发展规划
  9. 悬镜安全 | 第五期 全球一周安全情报(0820-0826)
  10. ssd nvme sata_NVMe SSD与传统SATA SSD