优化图形验证码

This commit is contained in:
none 2023-05-31 09:54:38 +08:00
parent 549f761fa9
commit 8da402255c
3 changed files with 14 additions and 90 deletions

12
pom.xml
View File

@ -75,13 +75,6 @@
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!--图形验证码依赖 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
@ -139,6 +132,11 @@
<artifactId>hutool-core</artifactId>
<version>5.8.16</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-captcha</artifactId>
<version>5.8.16</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>

View File

@ -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;
}
}

View File

@ -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;
}