New file |
| | |
| | | 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("验证码失效"); |
| | | } |
| | | |
| | | } |