use of com.pig4cloud.captcha.ArithmeticCaptcha in project pig by pig-mesh.
the class ImageCodeHandler method handle.
@Override
public Mono<ServerResponse> handle(ServerRequest serverRequest) {
ArithmeticCaptcha captcha = new ArithmeticCaptcha(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT);
String result = captcha.text();
// 保存验证码信息
Optional<String> randomStr = serverRequest.queryParam("randomStr");
redisTemplate.setKeySerializer(new StringRedisSerializer());
randomStr.ifPresent(s -> redisTemplate.opsForValue().set(CacheConstants.DEFAULT_CODE_KEY + s, result, SecurityConstants.CODE_TIME, TimeUnit.SECONDS));
// 转换流信息写出
FastByteArrayOutputStream os = new FastByteArrayOutputStream();
captcha.out(os);
return ServerResponse.status(HttpStatus.OK).contentType(MediaType.IMAGE_JPEG).body(BodyInserters.fromResource(new ByteArrayResource(os.toByteArray())));
}
use of com.pig4cloud.captcha.ArithmeticCaptcha in project albedo by somowhere.
the class AccoutResource method valicode.
@AnonymousAccess
@GetMapping(path = "/code/{randomStr}", produces = MediaType.IMAGE_JPEG_VALUE)
@Operation(summary = "获取验证码")
public void valicode(@PathVariable("randomStr") String randomStr, HttpServletResponse response) throws IOException {
ArgumentAssert.notEmpty(randomStr, "机器码不能为空");
response.setHeader("Cache-Control", "no-store, no-cache");
response.setHeader("Transfer-Encoding", "JPG");
response.setContentType("image/jpeg");
ArithmeticCaptcha captcha = new ArithmeticCaptcha(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT);
String result = captcha.text();
RedisUtil.setCacheString(CommonConstants.DEFAULT_CODE_KEY + randomStr, result, CommonConstants.DEFAULT_IMAGE_EXPIRE, TimeUnit.SECONDS);
// 创建输出流
ServletOutputStream out = response.getOutputStream();
captcha.out(out);
IoUtil.close(out);
}
Aggregations