1、本次使用的图形验证码为 kaptcha
<!--图形验证码 ################### 开始--> <!--<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>--> <!--图形验证码 ################### 结束-->
2、 设置图形验证码样式以及生成规则
2.1 yaml 文件增加
kaptcha:
#是否有边框,默认为yes,可选yes、no
border: "yes"
#//边框颜色
border.color: 105,179,90
textproducer:
font:
#验证码字体颜色
color: blue
#文本字符大小
size: 30
#文本字体样式
names: 宋体,楷体,微软雅黑
char:
#验证码文本字符长度
length: 4
image:
#图片宽度
width: 120
#图片高度
height: 40
session:
#存储session key
key: code
2.2 增加 KaptchaConfig

import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.util.Properties; /** * @program: jfxt-parent * @description: * @author: shiyanling * @create: 2021-01-25 19:35 **/ @Component @RefreshScope public class KaptchaConfig { @Value("${kaptcha.border}") private String border; @Value("${kaptcha.border.color}") private String borderColor; @Value("${kaptcha.textproducer.font.color}") private String textproducerFontColor; @Value("${kaptcha.textproducer.font.size}") private String textproducerFontSize; @Value("${kaptcha.textproducer.font.names}") private String textproducerFontNames; @Value("${kaptcha.textproducer.char.length}") private String textproducerCharLength; @Value("${kaptcha.image.width}") private String imageWidth; @Value("${kaptcha.image.height}") private String imageHeight; @Value("${kaptcha.session.key}") private String sessionKey; @Bean public DefaultKaptcha getDefaultKapcha() { DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); Properties properties = new Properties(); properties.setProperty("kaptcha.border", border); properties.setProperty("kaptcha.border.color", borderColor); properties.setProperty("kaptcha.textproducer.font.color", textproducerFontColor); properties.setProperty("kaptcha.textproducer.font.size", textproducerFontSize); properties.setProperty("kaptcha.textproducer.font.names", textproducerFontNames); properties.setProperty("kaptcha.textproducer.char.length", textproducerCharLength); properties.setProperty("kaptcha.image.width", imageWidth); properties.setProperty("kaptcha.image.height", imageHeight); properties.setProperty("kaptcha.session.key", sessionKey); Config config = new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; } }
2.3 设置handler

import com.google.code.kaptcha.Producer; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.sinoiov.jfxt.common.cache.util.RedisUtils; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ByteArrayResource; 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.UUID; /** * @program: jfxt-parent * @description: * @author: shiyanling * @create: 2021-01-25 16:28 **/ @Slf4j @Component @AllArgsConstructor public class CaptchaImageHandler implements HandlerFunction<ServerResponse> { //随机数code_key public static final String DEFAULT_CODE_KEY = "random_code_"; //// private final Producer producer; // private final DefaultKaptcha producer; @Autowired DefaultKaptcha defaultKaptcha; @Autowired RedisUtils redisUtils; @Override public Mono<ServerResponse> handle(ServerRequest serverRequest) { // 生成验证码 String capText = defaultKaptcha.createText(); // String capStr = capText.substring(0, capText.lastIndexOf("@")); // String code = capText.substring(capText.lastIndexOf("@") + 1); BufferedImage image = defaultKaptcha.createImage(capText); // 保存验证码信息 String randomStr = UUID.randomUUID().toString().replaceAll("-", ""); // redisTemplate.opsForValue().set(DEFAULT_CODE_KEY + randomStr, code, 60, TimeUnit.SECONDS); redisUtils.setEx(DEFAULT_CODE_KEY + randomStr, capText,60); // 转换流信息写出 FastByteArrayOutputStream os = new FastByteArrayOutputStream(); try { ImageIO.write(image, "jpg", os); } catch (IOException e) { log.error("ImageIO write error", e); return Mono.error(e); } return ServerResponse.status(HttpStatus.OK) .contentType(MediaType.IMAGE_JPEG) .header("randomstr", randomStr) .body(BodyInserters.fromResource(new ByteArrayResource(os.toByteArray()))); } }
2.4 设置网关路由
@Bean
public RouterFunction<ServerResponse> routeFunction(CaptchaImageHandler captchaImageHandler){
return RouterFunctions
.route(RequestPredicates.GET("/code")
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), captchaImageHandler::handle) ;
}
访问url http://ip:port/code 返回图形验证码