Search in sources :

Example 6 with AjaxResult

use of com.dimple.framework.web.domain.AjaxResult in project DimpleBlog by martin-chips.

the class CaptchaController method getCode.

/**
 * 生成验证码
 */
@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;
    redisCacheService.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
    // 生成图片
    int w = 111;
    int 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) {
        log.error(e.getMessage(), e);
        return AjaxResult.error(e.getMessage());
    } finally {
        stream.close();
    }
}
Also used : AjaxResult(com.dimple.framework.web.domain.AjaxResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

AjaxResult (com.dimple.framework.web.domain.AjaxResult)6 GetMapping (org.springframework.web.bind.annotation.GetMapping)5 LoginUser (com.dimple.framework.security.LoginUser)2 SysUser (com.dimple.project.system.domain.SysUser)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 Menu (com.dimple.project.system.domain.Menu)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1