html:

 <img src="/demo/getImage"  alt="" width="100" height="32" class="passcode" style="height:43px;cursor:pointer;" onclick="this.src=this.src+'?'">

控制器中 获取验证码图片:

//验证码图片@RequestMapping(value="getImage",method=RequestMethod.GET)public void authImage(HttpServletRequest request, HttpServletResponse response) throws IOException {response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0);response.setContentType("image/jpeg");// 生成随机字符串String verifyCode = VerifyCodeUtils.generateVerifyCode(4);// 建立sessionHttpSession session = request.getSession(true);// 删除之前的session.removeAttribute("verCode");session.removeAttribute("codeTime");session.setAttribute("verCode", verifyCode.toLowerCase());session.setAttribute("codeTime", LocalDateTime.now());// 生成图片int w = 100, h = 30;OutputStream out = response.getOutputStream();VerifyCodeUtils.outputImage(w, h, out, verifyCode);}

听说附近搬来了一个人类,小动物们变成长着各式各样耳朵和尾巴的人形去送礼物。 一个头发凌乱的刚睡醒的年轻人走了出来,看见他们有一丝惊讶。 说明来意后,小妖怪们排着队送起了礼物。 花栗鼠打了个饱嗝,把自己吃得剩半个的向日葵瓜子盘顶在头上递给了他。 尖耳朵的狐狸拿出一个鸟蛋,但在看见寻蛋而来的鹰爸爸时弃蛋而逃。 排队的时间太长,小仓鼠法力用尽,变回了原形。捧着花生太累了,他刚把花生藏在了脸颊里,就被追打狐狸的鹰爸爸不小心踩了一脚。 年轻人将趴在地上的它捧起来,笑看问:「要送我什么礼物呢?.」 小仓鼠刚刚不小心将花生吞了下去,它慌乱地用小爪子指指自己的脸颊,又指指自己的肚皮,发出吱吱吱的声音。 那年轻人揉揉它的脸,将呆住了的它放到了自己肩膀上: 「好吧,我接受你的以身相许了。」

学习太累来个故事吧
工具类:

package com.example.demo.util;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Random;import javax.imageio.ImageIO;public class VerifyCodeUtils{//使用到Algerian字体,系统里没有的话需要安装字体,字体只显示大写,去掉了1,0,i,o几个容易混淆的字符public static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";private static Random random = new Random();/*** 使用系统默认字符源生成验证码* @param verifySize    验证码长度* @return*/public static String generateVerifyCode(int verifySize){return generateVerifyCode(verifySize, VERIFY_CODES);}/*** 使用指定源生成验证码* @param verifySize    验证码长度* @param sources   验证码字符源* @return*/public static String generateVerifyCode(int verifySize, String sources){if(sources == null || sources.length() == 0){sources = VERIFY_CODES;}int codesLen = sources.length();Random rand = new Random(System.currentTimeMillis());StringBuilder verifyCode = new StringBuilder(verifySize);for(int i = 0; i < verifySize; i++){verifyCode.append(sources.charAt(rand.nextInt(codesLen-1)));}return verifyCode.toString();}/*** 生成随机验证码文件,并返回验证码值* @param w* @param h* @param outputFile* @param verifySize* @return* @throws IOException*/public static String outputVerifyImage(int w, int h, File outputFile, int verifySize) throws IOException{String verifyCode = generateVerifyCode(verifySize);outputImage(w, h, outputFile, verifyCode);return verifyCode;}/*** 输出随机验证码图片流,并返回验证码值* @param w* @param h* @param os* @param verifySize* @return* @throws IOException*/public static String outputVerifyImage(int w, int h, OutputStream os, int verifySize) throws IOException{String verifyCode = generateVerifyCode(verifySize);outputImage(w, h, os, verifyCode);return verifyCode;}/*** 生成指定验证码图像文件* @param w * @param h* @param outputFile* @param code* @throws IOException*/public static void outputImage(int w, int h, File outputFile, String code) throws IOException{if(outputFile == null){return;}File dir = outputFile.getParentFile();if(!dir.exists()){dir.mkdirs();}try{outputFile.createNewFile();FileOutputStream fos = new FileOutputStream(outputFile);outputImage(w, h, fos, code);fos.close();} catch(IOException e){throw e;}}/*** 输出指定验证码图片流* @param w* @param h* @param os* @param code* @throws IOException*/public static void outputImage(int w, int h, OutputStream os, String code) throws IOException{int verifySize = code.length();BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);Random rand = new Random();Graphics2D g2 = image.createGraphics();g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);Color[] colors = new Color[5];Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN,Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,Color.PINK, Color.YELLOW };float[] fractions = new float[colors.length];for(int i = 0; i < colors.length; i++){colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];fractions[i] = rand.nextFloat();}Arrays.sort(fractions);g2.setColor(Color.GRAY);// 设置边框色g2.fillRect(0, 0, w, h);Color c = getRandColor(200, 250);g2.setColor(c);// 设置背景色g2.fillRect(0, 2, w, h-4);//绘制干扰线Random random = new Random();g2.setColor(getRandColor(160, 200));// 设置线条的颜色for (int i = 0; i < 20; i++) {int x = random.nextInt(w - 1);int y = random.nextInt(h - 1);int xl = random.nextInt(6) + 1;int yl = random.nextInt(12) + 1;g2.drawLine(x, y, x + xl + 40, y + yl + 20);}// 添加噪点float yawpRate = 0.05f;// 噪声率int area = (int) (yawpRate * w * h);for (int i = 0; i < area; i++) {int x = random.nextInt(w);int y = random.nextInt(h);int rgb = getRandomIntColor();image.setRGB(x, y, rgb);}shear(g2, w, h, c);// 使图片扭曲g2.setColor(getRandColor(100, 160));int fontSize = h-4;Font font = new Font("Algerian", Font.ITALIC, fontSize);g2.setFont(font);char[] chars = code.toCharArray();for(int i = 0; i < verifySize; i++){AffineTransform affine = new AffineTransform();affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), (w / verifySize) * i + fontSize/2, h/2);g2.setTransform(affine);g2.drawChars(chars, i, 1, ((w-10) / verifySize) * i + 5, h/2 + fontSize/2 - 10);}g2.dispose();ImageIO.write(image, "jpg", os);}private static Color getRandColor(int fc, int bc) {if (fc > 255)fc = 255;if (bc > 255)bc = 255;int r = fc + random.nextInt(bc - fc);int g = fc + random.nextInt(bc - fc);int b = fc + random.nextInt(bc - fc);return new Color(r, g, b);}private static int getRandomIntColor() {int[] rgb = getRandomRgb();int color = 0;for (int c : rgb) {color = color << 8;color = color | c;}return color;}private static int[] getRandomRgb() {int[] rgb = new int[3];for (int i = 0; i < 3; i++) {rgb[i] = random.nextInt(255);}return rgb;}private static void shear(Graphics g, int w1, int h1, Color color) {shearX(g, w1, h1, color);shearY(g, w1, h1, color);}private static void shearX(Graphics g, int w1, int h1, Color color) {int period = random.nextInt(2);boolean borderGap = true;int frames = 1;int phase = random.nextInt(2);for (int i = 0; i < h1; i++) {double d = (double) (period >> 1)* Math.sin((double) i / (double) period+ (6.2831853071795862D * (double) phase)/ (double) frames);g.copyArea(0, i, w1, 1, (int) d, 0);if (borderGap) {g.setColor(color);g.drawLine((int) d, i, 0, i);g.drawLine((int) d + w1, i, w1, i);}}}private static void shearY(Graphics g, int w1, int h1, Color color) {int period = random.nextInt(40) + 10; // 50;boolean borderGap = true;int frames = 20;int phase = 7;for (int i = 0; i < w1; i++) {double d = (double) (period >> 1)* Math.sin((double) i / (double) period+ (6.2831853071795862D * (double) phase)/ (double) frames);g.copyArea(i, 0, 1, h1, 0, (int) d);if (borderGap) {g.setColor(color);g.drawLine(i, (int) d, i, 0);g.drawLine(i, (int) d + h1, i, h1);}}}}

spring boot验证码的实现相关推荐

  1. Spring boot验证码前后端验证

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. spring boot集成kaptcha图形验证码

    文章目录 环境变化引发的思考 web.xml设置kaptcha图形验证码 config设置kaptcha图形验证码 kaptcha图形验证码完整教程 kaptcha图形验证码属性表 Linux环境下k ...

  3. spring boot 实现发送邮箱验证码

    首先设置一下发件人邮箱,以QQ邮箱为例: 找到帐户,开启POP3/SMTP服务 然后会提醒你怎么去实现,验证完后,会给你一个授权码,记住这个授权码,后端spring boot 会用到 下面回到IDEA ...

  4. spring boot结合shiro实现用户-角色-权限的控制(包含用户名密码登陆和手机号验证码登陆)

    spring boot整合shiro实现权限校验 1.首先导入项目所需jar包 <parent><groupId>org.springframework.boot</gr ...

  5. spring boot 使用javaMailSender 发送qq邮箱验证码

    一 qq邮箱设置 1登录邮箱打开设置 2点击账号 3开启 POP3/SMTP服务 4保存密钥(这个一定要保存后面在java里面要调用) 二. spring boot代码设置 1.maven导入jar包 ...

  6. Spring Boot整合Shiro + JSP教程(用户认证,权限管理,图片验证码)

    在此首先感谢**编程不良人**up主提供的视频教程 代码都是跟着up的视频敲的,遇到的一些问题也是通过CSDN博主提供的教程解决的,在此也感谢那些提供bug解决方案的前辈们~ 项目完整代码已经发布到g ...

  7. Spring boot 发送手机验证码

    由于阿里云现在的短信签名无法通过申请,所以我这里选择了中国网建SMS短信平台(手机号注册即用,有免费赠送的几条短信测试) demo代码地址:https://github.com/mer97/sprin ...

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

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

  9. 【Spring Boot组件集成实战】集成Kaptcha谷歌验证码

    更多精彩内容,请访问 Spring Boot组件集成实战专栏 ! 推荐项目:一套基于Spring Boot+Layui的内容管理系统/快速开发脚手架(含完整的开发文档.演示网址等) 文章目录 1. 验 ...

最新文章

  1. 无法远程访问Mysql的解决方案
  2. python读csv-python读写csv文件
  3. 登陆页老是提示验证码错误,validate验证控件IE下用remote方法明明返回true 但是还是报错,提示验证码错误...
  4. Cookie操作以及如何在js中调用jsp变量
  5. 前端学习(1732):前端系列javascript之状态切换
  6. 分子进化和系统发育的基础知识
  7. 挖洞经验:通过Vimeo的文件上传功能发现其SSRF漏洞
  8. 1.4.2 solr字段类型--(1.4.2.4)使用Dates(日期)
  9. Java 网络编程(三) 创建和使用URL访问网络上的资源
  10. 将网站转换为应用程序的软件—“Unite”
  11. 软件工程经济学课后答案
  12. HashMap如何解决hash冲突?
  13. freemarker导出word文档中的复选框打钩功能
  14. 高级编程语言分类_高级编程语言的分类
  15. 基于StackGAN++、CycleGAN的图像生成工具,开源
  16. STM32F103 CubeMX:ETR外部脉冲计数器
  17. HDU - 6078 Wavel Sequence(动态规划+时间优化)
  18. 华为鸿蒙系统的技术特性,华为鸿蒙系统来了,还有这四大技术特性
  19. 如何开始人工智能之旅: 技术路径不只一条,您的选择也不只一个
  20. (在线教育)项目总结

热门文章

  1. 一、Arduino介绍
  2. 学生卡变成普通卡_15日前不审验 学生卡变成普通卡
  3. python将电视剧按收视率进行排序_Python爬虫实现数据可视化,卫视实时收视率对比,就是如此强大!...
  4. 认证系统之 devise 简单入门教程 (三)
  5. 夏日汽车保养 雨季汽车保养
  6. 怎么通过django模板输出双花括号{{}}
  7. Holt Winter时间序列模型
  8. 表达式运算(包含大整数加减乘)
  9. Android手机红外开发—点击和长按事件
  10. 怎么保护PDF的文字不被复制?