ImageCodeHandler.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2018-2025, lengleng All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the pig4cloud.com developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: lengleng (wangiegie@gmail.com)
  16. */
  17. package com.pig4cloud.pigx.gateway.handler;
  18. import com.google.code.kaptcha.Producer;
  19. import com.pig4cloud.pigx.common.core.constant.CommonConstants;
  20. import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
  21. import lombok.AllArgsConstructor;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.springframework.core.io.ByteArrayResource;
  24. import org.springframework.data.redis.core.RedisTemplate;
  25. import org.springframework.http.HttpStatus;
  26. import org.springframework.http.MediaType;
  27. import org.springframework.stereotype.Component;
  28. import org.springframework.util.FastByteArrayOutputStream;
  29. import org.springframework.web.reactive.function.BodyInserters;
  30. import org.springframework.web.reactive.function.server.HandlerFunction;
  31. import org.springframework.web.reactive.function.server.ServerRequest;
  32. import org.springframework.web.reactive.function.server.ServerResponse;
  33. import reactor.core.publisher.Mono;
  34. import javax.imageio.ImageIO;
  35. import java.awt.image.BufferedImage;
  36. import java.io.IOException;
  37. import java.util.concurrent.TimeUnit;
  38. /**
  39. * @author lengleng
  40. * @date 2018/7/5
  41. * 验证码生成逻辑处理类
  42. */
  43. @Slf4j
  44. @Component
  45. @AllArgsConstructor
  46. public class ImageCodeHandler implements HandlerFunction<ServerResponse> {
  47. private final Producer producer;
  48. private final RedisTemplate redisTemplate;
  49. @Override
  50. public Mono<ServerResponse> handle(ServerRequest serverRequest) {
  51. //生成验证码
  52. String text = producer.createText();
  53. BufferedImage image = producer.createImage(text);
  54. //保存验证码信息
  55. String randomStr = serverRequest.queryParam("randomStr").get();
  56. redisTemplate.opsForValue().set(CommonConstants.DEFAULT_CODE_KEY + randomStr, text
  57. , SecurityConstants.CODE_TIME, TimeUnit.SECONDS);
  58. // 转换流信息写出
  59. FastByteArrayOutputStream os = new FastByteArrayOutputStream();
  60. try {
  61. ImageIO.write(image, "jpeg", os);
  62. } catch (IOException e) {
  63. log.error("ImageIO write err", e);
  64. return Mono.error(e);
  65. }
  66. return ServerResponse
  67. .status(HttpStatus.OK)
  68. .contentType(MediaType.IMAGE_JPEG)
  69. .body(BodyInserters.fromResource(new ByteArrayResource(os.toByteArray())));
  70. }
  71. }