JAVA生成图形验证码并返回给前台,SpringBoot+vue

JAVA生成图形验证码并返回给前台,SpringBoot+vue
@GetMapping("/createCaptchaImage")
public ResultModel 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
    {
        ResultModel ajax = ResultModel.success();
        ajax.put("uuid", uuid);
        ajax.put("img", Base64.encode(stream.toByteArray()));
        return ajax;
    }
    catch (Exception e)
    {
        e.printStackTrace();
        return ResultModel.error(e.getMessage());
    }
    finally
    {
        stream.close();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_37999518/article/details/108448532
今日推荐