From 5d1b94ecc1269a95e5e5b76985d7b6fa0416cf85 Mon Sep 17 00:00:00 2001 From: 101captain <237651143@qq.com> Date: 星期三, 29 六月 2022 16:16:57 +0800 Subject: [PATCH] 验证码功能提交 --- springcloud_k8s_panzhihuazhihuishequ/applets_backstage/pom.xml | 5 + springcloud_k8s_panzhihuazhihuishequ/applets_backstage/src/main/java/com/panzhihua/applets_backstage/config/KaptchaConfig.java | 45 +++++++++++++++ springcloud_k8s_panzhihuazhihuishequ/applets_backstage/src/main/java/com/panzhihua/applets_backstage/api/KaphtchaApi.java | 80 ++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 0 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/applets_backstage/pom.xml b/springcloud_k8s_panzhihuazhihuishequ/applets_backstage/pom.xml index 315aae0..7a7e118 100644 --- a/springcloud_k8s_panzhihuazhihuishequ/applets_backstage/pom.xml +++ b/springcloud_k8s_panzhihuazhihuishequ/applets_backstage/pom.xml @@ -64,6 +64,11 @@ <artifactId>minio</artifactId> <version>6.0.8</version> </dependency> + <dependency> + <groupId>com.github.penggle</groupId> + <artifactId>kaptcha</artifactId> + <version>2.3.2</version> + </dependency> </dependencies> diff --git a/springcloud_k8s_panzhihuazhihuishequ/applets_backstage/src/main/java/com/panzhihua/applets_backstage/api/KaphtchaApi.java b/springcloud_k8s_panzhihuazhihuishequ/applets_backstage/src/main/java/com/panzhihua/applets_backstage/api/KaphtchaApi.java new file mode 100644 index 0000000..ed6a518 --- /dev/null +++ b/springcloud_k8s_panzhihuazhihuishequ/applets_backstage/src/main/java/com/panzhihua/applets_backstage/api/KaphtchaApi.java @@ -0,0 +1,80 @@ +package com.panzhihua.applets_backstage.api; + +import com.google.code.kaptcha.impl.DefaultKaptcha; +import com.panzhihua.common.controller.BaseController; +import com.panzhihua.common.model.vos.R; +import com.panzhihua.common.utlis.StringUtils; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +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.ByteArrayOutputStream; +import java.time.Duration; + +/** + * @author zzj + */ +@Api(tags = {"验证码接口"}) +@RestController +@RequestMapping("/kaphtcha/") +public class KaphtchaApi extends BaseController { + @Resource + private DefaultKaptcha defaultKaptcha; + @Resource + private StringRedisTemplate stringRedisTemplate; + + @ApiOperation("生成验证码") + @GetMapping("/verification") + public void defaultKaptcha(@RequestParam("uuid")String uuid,HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) + throws Exception { + byte[] captchaChallengeAsJpeg = null; + ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream(); + try { + // 生产验证码字符串并保存到session中,分布式环境存redis中 + String createText = defaultKaptcha.createText(); + stringRedisTemplate.opsForValue().set("verifyCode_"+uuid,createText, Duration.ofMinutes(5)); + // 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中 + BufferedImage challenge = defaultKaptcha.createImage(createText); + ImageIO.write(challenge, "jpg", jpegOutputStream); + + } catch (IllegalArgumentException e) { + httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND); + return; + } + + // 定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组 + captchaChallengeAsJpeg = jpegOutputStream.toByteArray(); + httpServletResponse.setHeader("Cache-Control", "no-store"); + httpServletResponse.setHeader("Pragma", "no-cache"); + httpServletResponse.setDateHeader("Expires", 0); + httpServletResponse.setContentType("image/jpeg"); + ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream(); + responseOutputStream.write(captchaChallengeAsJpeg); + responseOutputStream.flush(); + responseOutputStream.close(); + } + @ApiOperation("验证码核对") + @GetMapping("/checkVerifyCode") + public R checkVerifyCode(@RequestParam("verifyCode")String verifyCode,@RequestParam("uuid")String uuid){ + String text=stringRedisTemplate.opsForValue().get("verifyCode_"+uuid); + if(StringUtils.isNotEmpty(text)){ + if(verifyCode.equals(text)){ + stringRedisTemplate.delete("verifyCode_"+uuid); + return R.ok(); + } + return R.fail("验证码错误"); + } + return R.fail("验证码失效"); + } + +} diff --git a/springcloud_k8s_panzhihuazhihuishequ/applets_backstage/src/main/java/com/panzhihua/applets_backstage/config/KaptchaConfig.java b/springcloud_k8s_panzhihuazhihuishequ/applets_backstage/src/main/java/com/panzhihua/applets_backstage/config/KaptchaConfig.java new file mode 100644 index 0000000..354ee09 --- /dev/null +++ b/springcloud_k8s_panzhihuazhihuishequ/applets_backstage/src/main/java/com/panzhihua/applets_backstage/config/KaptchaConfig.java @@ -0,0 +1,45 @@ +package com.panzhihua.applets_backstage.config; + +import com.google.code.kaptcha.Constants; +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; + +/** + * @author zzj + */ +@Configuration +public class KaptchaConfig { + /** + * 验证码配置 + * @return + */ + @Bean + public DefaultKaptcha getDefaultKaptcha(){ + DefaultKaptcha defaultKaptcha=new DefaultKaptcha(); + Properties properties=new Properties(); + //是否有边框 + properties.setProperty(Constants.KAPTCHA_BORDER,"yes"); + //验证码文本颜色 + properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR,"red"); + //验证码图片宽度 + properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH,"180"); + //验证码图片高度 + properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT,"80"); + //文本字符大小 + properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE,"39"); + //验证码session的值 + properties.setProperty(Constants.KAPTCHA_SESSION_CONFIG_KEY,"kaptchaCode"); + //验证码文本长度 + properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH,"4"); + //字体 + properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, "宋体,楷体,微软雅黑"); + + Config config=new Config(properties); + defaultKaptcha.setConfig(config); + return defaultKaptcha; + } +} -- Gitblit v1.7.1