kaptcha 您所在的位置:网站首页 recaptcha怎么读 kaptcha

kaptcha

2023-08-18 16:02| 来源: 网络整理| 查看: 265

kaptcha——谷歌验证码工具 一、说在开始

小伙伴,你好! 这是本人第一次分享,记录一下自己在java中的学习过程。因为设计需求,在登录的时候需要进行图片验证登录。虽然也可以单独使用代码实现但是,当知道有更好的工具可以用的时候谁不想简单点呢?

二、工具简介 验证码的字体验证码字体的字体颜色验证码内容的范围(数字,字母,中文汉字!)验证码图片的大小,边框,边框粗细,边框颜色验证码的干扰线验证码的样式(鱼眼样式、3D、普通模糊、…) 三、用法

1、引入jar包 可以去官网http://code.google.com/p/kaptcha/下载jar,或者在pom.xml中导入

com.github.penggle kaptcha 2.3.2

2、编写配置文件 KaptChaConfig.class

import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import java.util.Properties; /** * @author dk * @version 1.0.0 * @ClassName KaptChaConfig.java * @Description TODO * @createTime 2021年11月20日 15:11:00 */ @Component public class KaptChaConfig { @Bean public DefaultKaptcha getDefaultKaptcha() { com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha(); Properties properties = new Properties(); // 图片边框 properties.setProperty("kaptcha.border", "no"); // 边框颜色 properties.setProperty("kaptcha.border.color", "black"); //边框厚度 properties.setProperty("kaptcha.border.thickness", "1"); // 图片宽 properties.setProperty("kaptcha.image.width", "120"); // 图片高 properties.setProperty("kaptcha.image.height", "60"); //图片实现类 properties.setProperty("kaptcha.producer.impl", "com.google.code.kaptcha.impl.DefaultKaptcha"); //文本实现类 properties.setProperty("kaptcha.textproducer.impl", "com.google.code.kaptcha.text.impl.DefaultTextCreator"); //文本集合,验证码值从此集合中获取 properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"); //验证码长度 properties.setProperty("kaptcha.textproducer.char.length", "4"); //字体 properties.setProperty("kaptcha.textproducer.font.names", "宋体"); //字体颜色 properties.setProperty("kaptcha.textproducer.font.color", "black"); //文字间隔 properties.setProperty("kaptcha.textproducer.char.space", "4"); //干扰实现类 properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.DefaultNoise"); //干扰颜色 properties.setProperty("kaptcha.noise.color", "blue"); //干扰图片样式 properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple"); //背景实现类 properties.setProperty("kaptcha.background.impl", "com.google.code.kaptcha.impl.DefaultBackground"); //背景颜色渐变,结束颜色 properties.setProperty("kaptcha.background.clear.to", "white"); //文字渲染器 properties.setProperty("kaptcha.word.impl", "com.google.code.kaptcha.text.impl.DefaultWordRenderer"); Config config = new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; } }

3、生成图片的方法 YanzhengmaUtil.class

import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; import org.springframework.util.FastByteArrayOutputStream; import com.google.code.kaptcha.impl.DefaultKaptcha; public class YanzhengmaUtil { /** * 生成验证码图片 * @param request 设置session * @param response 转成图片 * @param captchaProducer 生成图片方法类 * @param validateSessionKey session名称 * @throws Exception */ public static void validateCode(HttpServletRequest request, HttpServletResponse response, DefaultKaptcha captchaProducer, String validateSessionKey) throws Exception{ // 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"); // create the text for the image String capText = captchaProducer.createText(); // store the text in the session request.getSession().setAttribute(validateSessionKey, capText); System.out.println(request.getSession().getAttribute(validateSessionKey)); System.out.println(validateSessionKey+":"+capText); // create the image with the text BufferedImage bi = captchaProducer.createImage(capText); // OutputStream out = response.getOutputStream(); FastByteArrayOutputStream os = new FastByteArrayOutputStream(); // write the data out ImageIO.write(bi, "jpg", os); FileUtils.writeByteArrayToFile(new File("D:\\ice\\code\\ice_vue_test\\src\\pages\\login\\vercode.jpg"), os.toByteArray()); try { os.flush(); } finally { os.close(); } } }

D:\ice\code\ice_vue_test\src\pages\login\vercode.jpg 这个是设置你保存的本地地址

4、调用接口

这里就不用再多说了,直接调用方法就是。值得注意的就是 当某一个地方使用了springboot 的相关注解的时候,再另外一个地方如果想要使用该注解下的方法或者类,这是也需要加上Springboot与之相关的注解,不然会报错误。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有