package cn.cetc54.platform.base.controller.common; import cn.cetc54.platform.core.common.utils.ResultUtil; import cn.cetc54.platform.core.common.utils.CreateVerifyCode; import cn.cetc54.platform.core.common.vo.Result; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.UUID; import java.util.concurrent.TimeUnit; /** * @author */ @Api(description = "验证码接口") @RequestMapping("/platform/common/captcha") @RestController @Transactional public class CaptchaController { @Autowired private StringRedisTemplate redisTemplate; @RequestMapping(value = "/init",method = RequestMethod.GET) @ApiOperation(value = "初始化验证码") public Result initCaptcha() { String captchaId = UUID.randomUUID().toString().replace("-",""); String code = new CreateVerifyCode().randomStr(4); // 缓存验证码 redisTemplate.opsForValue().set(captchaId, code,2L, TimeUnit.MINUTES); return ResultUtil.data(captchaId); } @RequestMapping(value = "/draw/{captchaId}", method = RequestMethod.GET) @ApiOperation(value = "根据验证码ID获取图片") public void drawCaptcha(@PathVariable("captchaId") String captchaId, HttpServletResponse response) throws IOException { //得到验证码 生成指定验证码 String code=redisTemplate.opsForValue().get(captchaId); CreateVerifyCode vCode = new CreateVerifyCode(116,36,4,10,code); response.setContentType("image/png"); vCode.write(response.getOutputStream()); } }