diff --git a/pom.xml b/pom.xml index 50bf3a4..61e8723 100644 --- a/pom.xml +++ b/pom.xml @@ -75,13 +75,6 @@ spring-boot-starter-validation - - - com.github.penggle - kaptcha - 2.3.2 - - com.google.code.gson gson @@ -139,6 +132,11 @@ hutool-core 5.8.16 + + cn.hutool + hutool-captcha + 5.8.16 + org.springdoc diff --git a/src/main/java/xyz/playedu/api/config/KaptchaConfig.java b/src/main/java/xyz/playedu/api/config/KaptchaConfig.java deleted file mode 100644 index 786ac31..0000000 --- a/src/main/java/xyz/playedu/api/config/KaptchaConfig.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2023 杭州白书科技有限公司 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package xyz.playedu.api.config; - -import static com.google.code.kaptcha.Constants.*; - -import com.google.code.kaptcha.impl.DefaultKaptcha; -import com.google.code.kaptcha.util.Config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.util.Properties; - -@Configuration -public class KaptchaConfig { - - @Bean(name = "captchaProducer") - public DefaultKaptcha getKaptchaBean() { - DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); - Properties properties = new Properties(); - // 是否边框 - properties.setProperty(KAPTCHA_BORDER, "no"); - // 字符颜色 - properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "red"); - // 干扰线颜色 - properties.setProperty(KAPTCHA_NOISE_COLOR, "red"); - // 字符间距 - properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "5"); - // 图片宽度 - properties.setProperty(KAPTCHA_IMAGE_WIDTH, "120"); - // 图片高度 - properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "50"); - // 字符大小 - properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "40"); - // 字符长度 - properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4"); - // 字体样式 - properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier"); - - defaultKaptcha.setConfig(new Config(properties)); - - return defaultKaptcha; - } -} diff --git a/src/main/java/xyz/playedu/api/service/impl/ImageCaptchaServiceImpl.java b/src/main/java/xyz/playedu/api/service/impl/ImageCaptchaServiceImpl.java index 662b6de..a912782 100644 --- a/src/main/java/xyz/playedu/api/service/impl/ImageCaptchaServiceImpl.java +++ b/src/main/java/xyz/playedu/api/service/impl/ImageCaptchaServiceImpl.java @@ -15,27 +15,19 @@ */ package xyz.playedu.api.service.impl; -import com.google.code.kaptcha.impl.DefaultKaptcha; - -import jakarta.annotation.Resource; +import cn.hutool.captcha.CaptchaUtil; +import cn.hutool.captcha.LineCaptcha; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import org.springframework.util.FastByteArrayOutputStream; import xyz.playedu.api.service.ImageCaptchaService; import xyz.playedu.api.types.ImageCaptchaResult; -import xyz.playedu.api.util.Base64Util; import xyz.playedu.api.util.HelperUtil; import xyz.playedu.api.util.RedisUtil; -import java.awt.image.BufferedImage; -import java.io.IOException; - -import javax.imageio.ImageIO; - @Slf4j @Service public class ImageCaptchaServiceImpl implements ImageCaptchaService { @@ -46,29 +38,21 @@ public class ImageCaptchaServiceImpl implements ImageCaptchaService { @Value("${playedu.captcha.expire}") private Long ConfigExpire; - @Resource private DefaultKaptcha defaultKaptcha; - @Override - public ImageCaptchaResult generate() throws IOException { + public ImageCaptchaResult generate() { ImageCaptchaResult imageCaptcha = new ImageCaptchaResult(); - BufferedImage image; + + // 生成验证码 + LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(240, 100, 4, 1); // 图形验证码的key[api是无状态的需要key来锁定验证码的值] String randomKey = HelperUtil.randomString(16); imageCaptcha.setKey(randomKey); - - // 生成验证码 - imageCaptcha.setCode(defaultKaptcha.createText()); - image = defaultKaptcha.createImage(imageCaptcha.getCode()); + imageCaptcha.setCode(lineCaptcha.getCode()); + imageCaptcha.setImage("data:image/png;base64," + lineCaptcha.getImageBase64()); // 写入到redis中 - RedisUtil.set(getCacheKey(randomKey), imageCaptcha.getCode(), ConfigExpire); - - FastByteArrayOutputStream os = new FastByteArrayOutputStream(); - ImageIO.write(image, "png", os); - - String base64 = "data:image/png;base64," + Base64Util.encode(os.toByteArray()); - imageCaptcha.setImage(base64); + RedisUtil.set(getCacheKey(imageCaptcha.getKey()), imageCaptcha.getCode(), ConfigExpire); return imageCaptcha; }