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打印:

有些事情,没经历过不知道原理,没失败过不明白奥妙,没痛苦过不了解真谛。临渊羡鱼,不如退而结网!

浙公网安备 33010602011771号