Java 生成图形验证码

1、pom

<!--图片验证码-->
        <dependency>
            <groupId>com.github.whvcse</groupId>
            <artifactId>easy-captcha</artifactId>
            <version>1.6.2</version>
        </dependency>

或者

<dependency>
            <groupId>com.pig4cloud.plugin</groupId>
            <artifactId>captcha-core</artifactId>
            <version>2.2.2</version>
        </dependency>

 

2、demo

/**
     * 获取验证码
     */
    @GetMapping("/captcha")
    public void captcha(@RequestParam("traceId") String traceId, HttpServletResponse response) throws Exception {
        response.setContentType("image/gif");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        // 算数验证码
        ArithmeticCaptcha captcha = new ArithmeticCaptcha();
        log.info("{} => {}", traceId, captcha.text());
        // 验证码存入缓存 => 略
        // 输出图片流
        captcha.out(response.getOutputStream());
    }

3、调用

http://localhost:8080/index/captcha?traceId=aabbcc

前端显示:

 log打印:

 

posted @ 2024-02-19 09:15  都是城市惹的祸  阅读(8)  评论(0)    收藏  举报