12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- /*
- * Copyright (c) 2018-2025, lengleng All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * Neither the name of the pig4cloud.com developer nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- * Author: lengleng (wangiegie@gmail.com)
- */
- package com.pig4cloud.pigx.gateway.handler;
- import com.google.code.kaptcha.Producer;
- import com.pig4cloud.pigx.common.core.constant.CommonConstants;
- import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
- import lombok.AllArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.core.io.ByteArrayResource;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.MediaType;
- import org.springframework.stereotype.Component;
- import org.springframework.util.FastByteArrayOutputStream;
- import org.springframework.web.reactive.function.BodyInserters;
- import org.springframework.web.reactive.function.server.HandlerFunction;
- import org.springframework.web.reactive.function.server.ServerRequest;
- import org.springframework.web.reactive.function.server.ServerResponse;
- import reactor.core.publisher.Mono;
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.util.concurrent.TimeUnit;
- /**
- * @author lengleng
- * @date 2018/7/5
- * 验证码生成逻辑处理类
- */
- @Slf4j
- @Component
- @AllArgsConstructor
- public class ImageCodeHandler implements HandlerFunction<ServerResponse> {
- private final Producer producer;
- private final RedisTemplate redisTemplate;
- @Override
- public Mono<ServerResponse> handle(ServerRequest serverRequest) {
- //生成验证码
- String text = producer.createText();
- BufferedImage image = producer.createImage(text);
- //保存验证码信息
- String randomStr = serverRequest.queryParam("randomStr").get();
- redisTemplate.opsForValue().set(CommonConstants.DEFAULT_CODE_KEY + randomStr, text
- , SecurityConstants.CODE_TIME, TimeUnit.SECONDS);
- // 转换流信息写出
- FastByteArrayOutputStream os = new FastByteArrayOutputStream();
- try {
- ImageIO.write(image, "jpeg", os);
- } catch (IOException e) {
- log.error("ImageIO write err", e);
- return Mono.error(e);
- }
- return ServerResponse
- .status(HttpStatus.OK)
- .contentType(MediaType.IMAGE_JPEG)
- .body(BodyInserters.fromResource(new ByteArrayResource(os.toByteArray())));
- }
- }
|