From 8b44dbbeaf385ee72676f846422b34fda70a2556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Wed, 11 Dec 2019 12:53:20 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20Refactoring=20code.=20=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E8=AE=A1=E7=AE=97=E5=9B=BE=E5=BD=A2=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pigx-gateway/pom.xml | 4 +- .../gateway/config/KaptchaConfiguration.java | 86 ------------------- .../gateway/handler/ImageCodeHandler.java | 25 ++---- pom.xml | 2 +- 4 files changed, 11 insertions(+), 106 deletions(-) delete mode 100644 pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/KaptchaConfiguration.java diff --git a/pigx-gateway/pom.xml b/pigx-gateway/pom.xml index 909a774b5..5184f1b06 100644 --- a/pigx-gateway/pom.xml +++ b/pigx-gateway/pom.xml @@ -59,8 +59,8 @@ - com.github.axet - kaptcha + com.github.whvcse + easy-captcha ${kaptcha.version} diff --git a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/KaptchaConfiguration.java b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/KaptchaConfiguration.java deleted file mode 100644 index f85c96351..000000000 --- a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/KaptchaConfiguration.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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.config; - -import com.google.code.kaptcha.impl.DefaultKaptcha; -import com.google.code.kaptcha.util.Config; -import com.pig4cloud.pigx.common.core.constant.SecurityConstants; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.util.Properties; - -/** - * @author lengleng - * @date 2017-12-21 21:12:18 - */ -@Configuration -public class KaptchaConfiguration { - - private static final String KAPTCHA_BORDER = "kaptcha.border"; - private static final String KAPTCHA_TEXTPRODUCER_FONT_COLOR = "kaptcha.textproducer.font.color"; - private static final String KAPTCHA_TEXTPRODUCER_CHAR_SPACE = "kaptcha.textproducer.char.space"; - private static final String KAPTCHA_IMAGE_WIDTH = "kaptcha.image.width"; - private static final String KAPTCHA_IMAGE_HEIGHT = "kaptcha.image.height"; - private static final String KAPTCHA_TEXTPRODUCER_CHAR_LENGTH = "kaptcha.textproducer.char.length"; - private static final Object KAPTCHA_IMAGE_FONT_SIZE = "kaptcha.textproducer.font.size"; - - /** - * 默认生成图形验证码宽度 - */ - private static final String DEFAULT_IMAGE_WIDTH = "100"; - - /** - * 默认生成图像验证码高度 - */ - private static final String DEFAULT_IMAGE_HEIGHT = "40"; - - /** - * 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. - */ - private static final String DEFAULT_COLOR_FONT = "black"; - - /** - * 图片边框 - */ - private static final String DEFAULT_IMAGE_BORDER = "no"; - /** - * 默认图片间隔 - */ - private static final String DEFAULT_CHAR_SPACE = "5"; - /** - * 验证码文字大小 - */ - private static final String DEFAULT_IMAGE_FONT_SIZE = "30"; - - @Bean - public DefaultKaptcha producer() { - Properties properties = new Properties(); - properties.put(KAPTCHA_BORDER, DEFAULT_IMAGE_BORDER); - properties.put(KAPTCHA_TEXTPRODUCER_FONT_COLOR, DEFAULT_COLOR_FONT); - properties.put(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, DEFAULT_CHAR_SPACE); - properties.put(KAPTCHA_IMAGE_WIDTH, DEFAULT_IMAGE_WIDTH); - properties.put(KAPTCHA_IMAGE_HEIGHT, DEFAULT_IMAGE_HEIGHT); - properties.put(KAPTCHA_IMAGE_FONT_SIZE, DEFAULT_IMAGE_FONT_SIZE); - properties.put(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, SecurityConstants.CODE_SIZE); - Config config = new Config(properties); - DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); - defaultKaptcha.setConfig(config); - return defaultKaptcha; - } -} diff --git a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeHandler.java b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeHandler.java index a14f2ca0e..08bf1f210 100644 --- a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeHandler.java +++ b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeHandler.java @@ -17,10 +17,9 @@ package com.pig4cloud.pigx.gateway.handler; -import com.google.code.kaptcha.Producer; import com.pig4cloud.pigx.common.core.constant.CacheConstants; -import com.pig4cloud.pigx.common.core.constant.CommonConstants; import com.pig4cloud.pigx.common.core.constant.SecurityConstants; +import com.wf.captcha.ArithmeticCaptcha; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.core.io.ByteArrayResource; @@ -36,9 +35,6 @@ 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; /** @@ -50,30 +46,25 @@ import java.util.concurrent.TimeUnit; @Component @AllArgsConstructor public class ImageCodeHandler implements HandlerFunction { - private static final String JPEG = "jpeg"; - private final Producer producer; + private static final Integer DEFAULT_IMAGE_WIDTH = 100; + private static final Integer DEFAULT_IMAGE_HEIGHT = 40; private final RedisTemplate redisTemplate; @Override public Mono handle(ServerRequest serverRequest) { - //生成验证码 - String text = producer.createText(); - BufferedImage image = producer.createImage(text); + ArithmeticCaptcha captcha = new ArithmeticCaptcha(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT); + + String result = captcha.text(); //保存验证码信息 String randomStr = serverRequest.queryParam("randomStr").get(); redisTemplate.setKeySerializer(new StringRedisSerializer()); - redisTemplate.opsForValue().set(CacheConstants.DEFAULT_CODE_KEY + randomStr, text + redisTemplate.opsForValue().set(CacheConstants.DEFAULT_CODE_KEY + randomStr, result , 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); - } + captcha.out(os); return ServerResponse .status(HttpStatus.OK) diff --git a/pom.xml b/pom.xml index c2912c778..f7515aa08 100644 --- a/pom.xml +++ b/pom.xml @@ -42,7 +42,7 @@ 1.8 2.2.0 5.0.6 - 0.0.9 + 1.6.2 2.9.2 1.9.6 2.10.0