¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.web.controller.common; |
| | | |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.util.concurrent.TimeUnit; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import com.ruoyi.common.constant.Constants; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.core.redis.RedisCache; |
| | | import com.ruoyi.common.utils.VerifyCodeUtils; |
| | | import com.ruoyi.common.utils.sign.Base64; |
| | | import com.ruoyi.common.utils.uuid.IdUtils; |
| | | |
| | | /** |
| | | * éªè¯ç æä½å¤ç |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @RestController |
| | | public class CaptchaController |
| | | { |
| | | @Autowired |
| | | private RedisCache redisCache; |
| | | |
| | | /** |
| | | * çæéªè¯ç |
| | | */ |
| | | @GetMapping("/captchaImage") |
| | | public AjaxResult getCode(HttpServletResponse response) throws IOException |
| | | { |
| | | // çæéæºå串 |
| | | String verifyCode = VerifyCodeUtils.generateVerifyCode(4); |
| | | // å¯ä¸æ è¯ |
| | | String uuid = IdUtils.simpleUUID(); |
| | | String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid; |
| | | |
| | | redisCache.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); |
| | | // çæå¾ç |
| | | int w = 111, h = 36; |
| | | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| | | VerifyCodeUtils.outputImage(w, h, stream, verifyCode); |
| | | try |
| | | { |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | ajax.put("uuid", uuid); |
| | | ajax.put("img", Base64.encode(stream.toByteArray())); |
| | | return ajax; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | e.printStackTrace(); |
| | | return AjaxResult.error(e.getMessage()); |
| | | } |
| | | finally |
| | | { |
| | | stream.close(); |
| | | } |
| | | } |
| | | } |