Search in sources :

Example 1 with ArithmeticCaptcha

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())));
}
Also used : ArithmeticCaptcha(com.pig4cloud.captcha.ArithmeticCaptcha) StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) FastByteArrayOutputStream(org.springframework.util.FastByteArrayOutputStream) ByteArrayResource(org.springframework.core.io.ByteArrayResource)

Example 2 with ArithmeticCaptcha

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);
}
Also used : ArithmeticCaptcha(com.pig4cloud.captcha.ArithmeticCaptcha) ServletOutputStream(javax.servlet.ServletOutputStream) AnonymousAccess(com.albedo.java.common.core.annotation.AnonymousAccess) Operation(io.swagger.v3.oas.annotations.Operation)

Aggregations

ArithmeticCaptcha (com.pig4cloud.captcha.ArithmeticCaptcha)2 AnonymousAccess (com.albedo.java.common.core.annotation.AnonymousAccess)1 Operation (io.swagger.v3.oas.annotations.Operation)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)1 FastByteArrayOutputStream (org.springframework.util.FastByteArrayOutputStream)1