当前位置: 代码迷 >> 综合 >> 使用kaptcha生成验证码图片-WebFlux(RouterFunction实现)篇
  详细解决方案

使用kaptcha生成验证码图片-WebFlux(RouterFunction实现)篇

热度:35   发布时间:2024-02-24 00:15:08.0

Kaptcha 是一个可高度配置的实用验证码生成工具,可自由配置的选项有很多,比如验证码的字体、字体的大小、颜色等。可以制作成一个可以重复使用的便捷小工具。如果是使用springMVC方式的接口可以参考使用kaptcha生成验证码图片-springMVC篇。

1、导入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!--验证码 --><dependency><groupId>com.github.penggle</groupId><artifactId>kaptcha</artifactId><version>2.3.2</version>
</dependency>

注意这里的spring-boot-starter-webfluxspring-boot-starter-web不可以同时引入,我们引入webflux就可以了,否则RouterFunction会访问不到导致404错误。

2、编写一个生成数字运算的验证码类
/*** 一个生成数字运算的验证码类*/
@Component
public class KaptchaTextCreator extends DefaultTextCreator
{private static final String[] CNUMBERS = "0,1,2,3,4,5,6,7,8,9,10".split(",");@Overridepublic String getText(){Integer result = 0;Random random = new Random();int x = random.nextInt(10);                 // 制造两个随机数  x,yint y = random.nextInt(10);StringBuilder suChinese = new StringBuilder();// Math.round 是四舍五入  Math.random随机一个0-1的浮点数   randomoperands的值可能为0,1,2int randomoperands = (int) Math.round(Math.random() * 2);if (randomoperands == 0){// 对两个随机数进行乘法运算result = x * y;suChinese.append(CNUMBERS[x]);suChinese.append("*");suChinese.append(CNUMBERS[y]);}else if (randomoperands == 1){// 对两个随机数进行除法运算(如果这两个数相除可以没有余数的话,否则还是进行加法)if (!(x == 0) && y % x == 0){result = y / x;suChinese.append(CNUMBERS[y]);suChinese.append("/");suChinese.append(CNUMBERS[x]);}else{result = x + y;suChinese.append(CNUMBERS[x]);suChinese.append("+");suChinese.append(CNUMBERS[y]);}}else if (randomoperands == 2){// 对两个随机数进行减法运算,用大的减去小的if (x >= y){result = x - y;suChinese.append(CNUMBERS[x]);suChinese.append("-");suChinese.append(CNUMBERS[y]);}else{result = y - x;suChinese.append(CNUMBERS[y]);suChinese.append("-");suChinese.append(CNUMBERS[x]);}}else{// 其余情况全部采用加法运算result = x + y;suChinese.append(CNUMBERS[x]);suChinese.append("+");suChinese.append(CNUMBERS[y]);}suChinese.append("=?@" + result);return suChinese.toString();}
}
3、编写验证码配置
@Configuration
public class CaptchaConfig
{@Bean(name = "captchaProducerMath")public DefaultKaptcha getKaptchaBeanMath(){DefaultKaptcha defaultKaptcha = new DefaultKaptcha();Properties properties = new Properties();// 是否有边框 默认为true 我们可以自己设置yes,noproperties.setProperty("kaptcha.border", "yes");// 边框颜色 默认为Color.BLACKproperties.setProperty("kaptcha.border.color", "105,179,90");// 验证码文本字符颜色 默认为Color.BLACKproperties.setProperty("kaptcha.textproducer.font.color", "blue");// 验证码图片宽度 默认为200properties.setProperty("kaptcha.image.width", "160");// 验证码图片高度 默认为50properties.setProperty("kaptcha.image.height", "60");// 验证码文本字符大小 默认为40properties.setProperty("kaptcha.textproducer.font.size", "35");// KAPTCHA_SESSION_KEYproperties.setProperty("kaptcha.session.key", "kaptchaCodeMath");// 验证码文本生成器properties.setProperty("kaptcha.textproducer.impl", "com.example.bootMp.webflux.config.KaptchaTextCreator");// 验证码文本字符间距 默认为2properties.setProperty("kaptcha.textproducer.char.space", "3");// 验证码文本字符长度 默认为5properties.setProperty("kaptcha.textproducer.char.length", "6");// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1,// fontSize)properties.setProperty("kaptcha.textproducer.font.names", "Arial,Courier");// 验证码噪点颜色 默认为Color.BLACKproperties.setProperty("kaptcha.noise.color", "white");// 干扰实现类properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple// 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy// 阴影com.google.code.kaptcha.impl.ShadowGimpyproperties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");Config config = new Config(properties);defaultKaptcha.setConfig(config);return defaultKaptcha;}
}

这里一定要注意一个配置,不可以直接照抄:

properties.setProperty("kaptcha.textproducer.impl", "com.example.bootMp.webflux.config.KaptchaTextCreator");

这个kaptcha.textproducer.impl的值就是要配置成以上生成数字运算的验证码类的名称。

4、编写处理生成图片逻辑的handler
@Slf4j
@Component
@AllArgsConstructor
public class ImgCodeHandler implements HandlerFunction<ServerResponse>
{private final Producer producer;private final StringRedisTemplate redisTemplate;@Overridepublic Mono<ServerResponse> handle(ServerRequest serverRequest){// 生成验证码String capText = producer.createText();String capStr = capText.substring(0, capText.lastIndexOf("@"));String code = capText.substring(capText.lastIndexOf("@") + 1);BufferedImage image = producer.createImage(capStr);// 保存验证码信息String randomStr = UUID.randomUUID().toString().replaceAll("-", "");redisTemplate.opsForValue().set(Constants.DEFAULT_CODE_KEY + randomStr, code, 60, TimeUnit.SECONDS);// 转换流信息写出FastByteArrayOutputStream os = new FastByteArrayOutputStream();try{ImageIO.write(image, "jpg", os);}catch (IOException e){log.error("ImageIO write err", e);return Mono.error(e);}return ServerResponse.status(HttpStatus.OK).contentType(MediaType.IMAGE_JPEG).header("randomstr", randomStr).body(BodyInserters.fromResource(new ByteArrayResource(os.toByteArray())));}
}
5、编写路由配置信息类(相当于springMVC中的接口路径)
@Configuration
@AllArgsConstructor
public class RouterFunctionConfiguration
{private final ImgCodeHandler imgCodeHandler;private final PingHandler pingHandler;@Beanpublic RouterFunction<?> routerFunction(){   // 注册 RouterFunctionreturn RouterFunctions.route(RequestPredicates.GET("/ping").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)),pingHandler).andRoute(RequestPredicates.GET("/codeweb").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), imgCodeHandler);}
}

访问写好的接口,生成的验证码如图所示:
在这里插入图片描述