虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > Java编程 > spring boot实现验证码功能

spring boot实现验证码功能
类别:Java编程   作者:码皇   来源:互联网   点击:

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。这篇文章主要介绍了spring boot实现验证码功能,需要的朋友可以参考下

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

下面通过实例代码给大家介绍spring boot实现验证码功能,具体详情如下所示:

1.建立工具类,配置验证码相关参数

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    /** * @author ld * @date 2017年11月6日 * @param * @desc 图形验证码生成 * */ public class VerifyUtil {
    // 验证码字符集 private static final char[] chars = {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
    ;
    // 字符数量 private static final int SIZE = 4;
    // 干扰线数量 private static final int LINES = 5;
    // 宽度 private static final int WIDTH = 80;
    // 高度 private static final int HEIGHT = 40;
    // 字体大小 private static final int FONT_SIZE = 30;
    /** * 生成随机验证码及图片 * Object[0]:验证码字符串; * Object[1]:验证码图片。 */ public static Object[] createImage() {
    StringBuffer sb = new StringBuffer();
    // 1.创建空白图片 BufferedImage image = new BufferedImage( WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    // 2.获取图片画笔 Graphics graphic = image.getGraphics();
    // 3.设置画笔颜色 graphic.setColor(Color.LIGHT_GRAY);
    // 4.绘制矩形背景 graphic.fillRect(0, 0, WIDTH, HEIGHT);
    // 5.画随机字符 Random ran = new Random();
    for (int i = 0;
    i <SIZE;
    i++) {
    // 取随机字符索引 int n = ran.nextInt(chars.length);
    // 设置随机颜色 graphic.setColor(getRandomColor());
    // 设置字体大小 graphic.setFont(new Font( null, Font.BOLD + Font.ITALIC, FONT_SIZE));
    // 画字符 graphic.drawString( chars[n] + "", i * WIDTH / SIZE, HEIGHT*2/3);
    // 记录字符 sb.append(chars[n]);
    }
    // 6.画干扰线 for (int i = 0;
    i < LINES;
    i++) {
    // 设置随机颜色 graphic.setColor(getRandomColor());
    // 随机画线 graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
    }
    // 7.返回验证码和图片 return new Object[]{
    sb.toString(), image}
    ;
    }
    /** * 随机取色 */ public static Color getRandomColor() {
    Random ran = new Random();
    Color color = new Color(ran.nextInt(256), ran.nextInt(256), ran.nextInt(256));
    return color;
    }
    }

2.接口

    @RequestMapping(value="/createValicode",method=RequestMethod.GET) public void valicode(HttpServletResponse response,HttpSession session) throws Exception{
    //利用图片工具生成图片 //第一个参数是生成的验证码,第二个参数是生成的图片 Object[] objs = VerifyUtil.createImage();
    //将验证码存入Session session.setAttribute("imageCode",objs[0]);
    //将图片输出给浏览器 BufferedImage image = (BufferedImage) objs[1];
    response.setContentType("image/png");
    OutputStream os = response.getOutputStream();
    ImageIO.write(image, "png", os);
    }

3.测试页面调用

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title>hello</title> </head> <body> <h1 th:text="${
    info}
    " /> <div> <!-- <img alt="这是图片" src="/img/001.png"/> --> <img alt="验证码" onclick = "this.src='/iot-frame/createValicode?' + Math.floor(Math.random() * 100)" src="/iot-frame/createValicode" /> </div> <form action="imgvrifyControllerDefaultKaptcha"> <input type="text" name="vrifyCode" /> <input type="submit" value="提交"></input> </form> </body> </html>

总结

以上所述是小编给大家介绍的spring boot实现验证码功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:

  • 实例详解Spring Boot实战之Redis缓存登录验证码
  • Springboot实现阿里云通信短信服务有关短信验证码的发送功能
  • SpringBoot实现短信验证码校验方法思路详解
  • 登陆验证码kaptcha结合spring boot的用法详解
  • Spring Boot 验证码的生成和验证详解
相关热词搜索: spring boot 验证码 springboot实现验证码