欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot实现验证码功能

程序员文章站 2023-11-24 08:44:58
spring boot是由pivotal团队提供的全新框架,其设计目的是用来简化新spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不...

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实现验证码功能,希望对大家有所帮助