java生成验证码图片

发布时间 2023-06-08 10:34:11作者: KeepSmiling_me
    <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.7</version>
        </dependency>

   public static final TimedCache<String, String> CODE_CACHE = CacheUtil.newTimedCache(3000);
    @SneakyThrows
    public void getCode(@RequestParam String username, HttpServletResponse response) {

        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(12,50,4,2);
        //图形验证码结果存缓存
        CODE_CACHE.put(username, captcha.getCode());
        //图形验证码写出到流
        captcha.write(response.getOutputStream());
    }