1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 谷歌验证码kaptcha生成工具类

谷歌验证码kaptcha生成工具类

时间:2023-12-04 10:05:29

相关推荐

谷歌验证码kaptcha生成工具类

谷歌验证码生成工具类

1、引入依赖2、验证码配置类3、controller层4、测试5、目录结构

1、引入依赖

<!-- google kaptcha依赖 --><dependency><groupId>com.github.axet</groupId><artifactId>kaptcha</artifactId><version>0.0.9</version></dependency>

2、验证码配置类

package com.tang.config;import com.google.code.kaptcha.impl.DefaultKaptcha;import com.google.code.kaptcha.util.Config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.Properties;/**** 验证码配置类** @program: yeb* @description:* @author: xiaokaixin* @create: -10-18 12:43**/@Configurationpublic class CaptchaConfig {@Beanpublic DefaultKaptcha defaultKaptcha(){//验证码生成器DefaultKaptcha defaultKaptcha=new DefaultKaptcha();//配置Properties properties = new Properties();//是否有边框properties.setProperty("kaptcha.border", "yes");//设置边框颜色properties.setProperty("kaptcha.border.color", "105,179,90");//边框粗细度,默认为1// properties.setProperty("kaptcha.border.thickness","1");//验证码properties.setProperty("kaptcha.session.key","code");//验证码文本字符颜色 默认为黑色properties.setProperty("kaptcha.textproducer.font.color", "blue");//设置字体样式properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");//字体大小,默认40properties.setProperty("kaptcha.textproducer.font.size", "30");//验证码文本字符内容范围 默认为abced2345678gfynmnpwx// properties.setProperty("kaptcha.textproducer.char.string", "");//字符长度,默认为5properties.setProperty("kaptcha.textproducer.char.length", "4");//字符间距 默认为2properties.setProperty("kaptcha.textproducer.char.space", "4");//验证码图片宽度 默认为200properties.setProperty("kaptcha.image.width", "100");//验证码图片高度 默认为40properties.setProperty("kaptcha.image.height", "40");Config config = new Config(properties);defaultKaptcha.setConfig(config);return defaultKaptcha;}}

3、controller层

生成验证码并响应给客户端并且把验证码的信息保存到session中

package com.tang.controller;import com.google.code.kaptcha.impl.DefaultKaptcha;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import javax.imageio.ImageIO;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.awt.image.BufferedImage;import java.io.IOException;/*** 验证码* @program: yeb* @description:* @author: xiaokaixin* @create: -10-18 12:48**/@RestControllerpublic class CaptchaController {@Autowiredprivate DefaultKaptcha defaultKaptcha;@GetMapping(value = "/captcha",produces = "image/jpeg")public void captcha(HttpServletRequest request, HttpServletResponse response){// 定义response输出类型为image/jpeg类型response.setDateHeader("Expires", 0);// Set standard HTTP/1.1 no-cache headers.response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");// Set IE extended HTTP/1.1 no-cache headers (use addHeader).response.addHeader("Cache-Control", "post-check=0, pre-check=0");// Set standard HTTP/1.0 no-cache header.response.setHeader("Pragma", "no-cache");// return a jpegresponse.setContentType("image/jpeg");//-------------------生成验证码 begin --------------------------//获取验证码文本内容String text = defaultKaptcha.createText();System.out.println("验证码内容:"+text);//将验证码文本内容放入sessionrequest.getSession().setAttribute("captcha",text);BufferedImage image = defaultKaptcha.createImage(text);ServletOutputStream outputStream = null;try {outputStream = response.getOutputStream();//输出流输出图片,格式为jpgImageIO.write(image,"jpg",outputStream);outputStream.flush();} catch (IOException e) {e.printStackTrace();}finally {if (null!=outputStream){try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}}//-------------------生成验证码 end --------------------------}}

4、测试

输入http://127.0.0.1:8080/captcha回车

5、目录结构

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。