kaptcha生成验证码、图片通过流的形式响应到前端

阅读: 评论:0

kaptcha生成验证码、图片通过流的形式响应到前端

kaptcha生成验证码、图片通过流的形式响应到前端

一、简介

二、代码实现

2.1 Pom依赖

 <!-- 验证码 --><dependency><groupId>com.github.penggle</groupId><artifactId>kaptcha</artifactId><version>2.3.2</version><exclusions><exclusion><artifactId>javax.servlet-api</artifactId><groupId>javax.servlet</groupId></exclusion></exclusions></dependency>

2.2 Config配置类

CaptchaConfig验证码配置

package com.fig;le.code.kaptcha.impl.DefaultKaptcha;
le.code.kaptcha.util.Config;
import t.annotation.Bean;
import t.annotation.Configuration;import java.util.Properties;import le.code.kaptcha.Constants.*;/*** 验证码配置* @Date 2021年09月15日16:46:08* @Author  LiuShihao*/
@Configuration
public class CaptchaConfig {@Bean(name = "captchaProducer")public DefaultKaptcha getKaptchaBean() {DefaultKaptcha defaultKaptcha = new DefaultKaptcha();Properties properties = new Properties();// 是否有边框 默认为true 我们可以自己设置yes,noproperties.setProperty(KAPTCHA_BORDER, "yes");// 验证码文本字符颜色 默认为Color.BLACKproperties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");// 验证码图片宽度 默认为200properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");// 验证码图片高度 默认为50properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");// 验证码文本字符大小 默认为40properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38");// KAPTCHA_SESSION_KEYproperties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");// 验证码文本字符长度 默认为5properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");// 图片样式 水纹de.kaptcha.impl.WaterRipple 鱼眼de.kaptcha.impl.FishEyeGimpy 阴影de.kaptcha.impl.ShadowGimpyproperties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, &#le.code.kaptcha.impl.ShadowGimpy");Config config = new Config(properties);defaultKaptcha.setConfig(config);return defaultKaptcha;}@Bean(name = "captchaProducerMath")public DefaultKaptcha getKaptchaBeanMath() {DefaultKaptcha defaultKaptcha = new DefaultKaptcha();Properties properties = new Properties();// 是否有边框 默认为true 我们可以自己设置yes,noproperties.setProperty(KAPTCHA_BORDER, "yes");// 边框颜色 默认为Color.BLACKproperties.setProperty(KAPTCHA_BORDER_COLOR, "105,179,90");// 验证码文本字符颜色 默认为Color.BLACKproperties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");// 验证码图片宽度 默认为200properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");// 验证码图片高度 默认为50properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");// 验证码文本字符大小 默认为40properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "35");// KAPTCHA_SESSION_KEYproperties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeMath");// 验证码文本生成器properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.fig.KaptchaTextCreator");// 验证码文本字符间距 默认为2properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");// 验证码文本字符长度 默认为5properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "6");// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");// 验证码噪点颜色 默认为Color.BLACKproperties.setProperty(KAPTCHA_NOISE_COLOR, "white");// 干扰实现类properties.setProperty(KAPTCHA_NOISE_IMPL, &#le.code.kaptcha.impl.NoNoise");// 图片样式 水纹de.kaptcha.impl.WaterRipple 鱼眼de.kaptcha.impl.FishEyeGimpy 阴影de.kaptcha.impl.ShadowGimpyproperties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, &#le.code.kaptcha.impl.ShadowGimpy");Config config = new Config(properties);defaultKaptcha.setConfig(config);return defaultKaptcha;}
}

KaptchaTextCreator验证码文本生成器

package com.fig;le.impl.DefaultTextCreator;import java.util.Random;/*** 验证码文本生成器* @Date 2021年09月15日16:46:08* @Author  LiuShihao*/
public class KaptchaTextCreator extends DefaultTextCreator {private static final String[] CNUMBERS = "0,1,2,3,4,5,6,7,8,9,10".split(",");@Overridepublic String getText() {Integer result = 0;Random random = new Random();int x = Int(10);int y = Int(10);StringBuilder suChinese = new StringBuilder();int randomoperands = (int) und(Math.random() * 2);if (randomoperands == 0) {result = x * y;suChinese.append(CNUMBERS[x]);suChinese.append("*");suChinese.append(CNUMBERS[y]);}else if (randomoperands == 1) {if (!(x == 0) && y % x == 0) {result = y / x;suChinese.append(CNUMBERS[y]);suChinese.append("/");suChinese.append(CNUMBERS[x]);}else {result = x + y;suChinese.append(CNUMBERS[x]);suChinese.append("+");suChinese.append(CNUMBERS[y]);}}else if (randomoperands == 2) {if (x >= y) {result = x - y;suChinese.append(CNUMBERS[x]);suChinese.append("-");suChinese.append(CNUMBERS[y]);}else {result = y - x;suChinese.append(CNUMBERS[y]);suChinese.append("-");suChinese.append(CNUMBERS[x]);}}else {result = x + y;suChinese.append(CNUMBERS[x]);suChinese.append("+");suChinese.append(CNUMBERS[y]);}suChinese.append("=?@" + result);String();}
}

2.3 CaptchaController控制层

此处生成验证码Code、UUID、并存入Redis中
在登录时进行验证UUID和Code与Redis中的是否一致。

package com.ller;le.code.kaptcha.Producer;
import stant.Constants;
import dis.RedisCache;
import com.lshmon.util.AjaxResult;
import com.lshmon.util.Base64;
import com.lshmon.util.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.StringRedisTemplate;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
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.*;
import java.util.Map;
import urrent.TimeUnit;/*** 验证码操作处理* @Date 2021年09月15日16:46:08* @Author  LiuShihao*/
@RestController
public class CaptchaController {@Resource(name = "captchaProducer")private Producer captchaProducer;@Resource(name = "captchaProducerMath")private Producer captchaProducerMath;@Autowiredprivate RedisCache redisCache;@AutowiredStringRedisTemplate stringRedisTemplate;// 验证码类型@Value("${pe}")private String captchaType;/*** 生成验证码* @param response* @return UUID 本次图片验证码唯一标识* @return img  图片验证码Base64编码* @throws IOException*/@GetMapping("/captchaImage")public AjaxResult getCode(HttpServletResponse response) throws IOException {// 保存验证码信息String uuid = IdUtils.simpleUUID();String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;String capStr = null, code = null;BufferedImage image = null;// 生成验证码if ("math".equals(captchaType)) {String capText = ateText();capStr = capText.substring(0, capText.lastIndexOf("@"));code = capText.substring(capText.lastIndexOf("@") + 1);image = ateImage(capStr);}else if ("char".equals(captchaType)) {capStr = code = ateText();image = ateImage(capStr);}System.out.println("code:"+code+" , uuid:"+uuid);
//        stringRedisTemplate.opsForValue().set(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);// 转换流信息写出FastByteArrayOutputStream os = new FastByteArrayOutputStream();try {ImageIO.write(image, "jpg", os);}catch (IOException e) {(e.getMessage());}System.out.println("img:"&#ByteArray()));AjaxResult ajax = AjaxResult.success();ajax.put("uuid", uuid);ajax.put("img", ByteArray()));return ajax;}

直接返回验证码图片至浏览器

主要就是通过FileInputStream和响应的输出流,并设置响应的请求头:

/*** 直接将图片的文件流响应到前端* @param request* @param response* @throws Exception*/@GetMapping("/getCaptchaImage")public void getCaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {File file = new File("./img.jpg");FileInputStream fis = new FileInputStream(file);ServletOutputStream os = OutputStream();response.addHeader("Content-Length", "" + file.length());response.setContentType("image/jpg");byte[] bytes = new byte[1024];while (ad(bytes) != -1){os.write(bytes);}fis.close();os.close();System.out.println("===完成===");}

2.4 yml配置

captcha:# 验证码类型 math 数组计算 char 字符验证type: char

三、测试


并且两分钟后失效

四、源码

其中的工具类就不在贴出来了,代码已上传仓库。

本文发布于:2024-02-01 03:59:33,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170673117533694.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:验证码   过流   形式   图片   kaptcha
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23