Skip to content

Commit

Permalink
refactor: 适配 ContiNew Starter 图形验证码
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Nov 24, 2023
1 parent b7fc5a5 commit 8a11a02
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 152 deletions.
16 changes: 8 additions & 8 deletions continew-admin-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
<artifactId>continew-starter-cache-redisson</artifactId>
</dependency>

<!-- ContiNew Starter JSON 模块 - Jackson -->
<!-- ContiNew Starter 验证码模块 - 图形验证码 -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-json-jackson</artifactId>
<artifactId>continew-starter-captcha-graphic</artifactId>
</dependency>

<!-- ContiNew Starter API 文档模块 -->
Expand All @@ -41,6 +41,12 @@
<artifactId>continew-starter-api-doc</artifactId>
</dependency>

<!-- ContiNew Starter JSON 模块 - Jackson -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-json-jackson</artifactId>
</dependency>

<!-- ################ Spring Boot 相关 ################ -->
<!-- Java 邮件支持 -->
<dependency>
Expand Down Expand Up @@ -106,11 +112,5 @@
<groupId>net.dreamlu</groupId>
<artifactId>mica-ip2region</artifactId>
</dependency>

<!-- Easy Captcha(Java 图形验证码,支持 gif、中文、算术等类型,可用于 Java Web、JavaSE 等项目) -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,10 @@

package top.charles7c.cnadmin.common.config.mybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;

import cn.hutool.core.net.NetUtil;

/**
* MyBatis Plus 配置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@
import java.awt.*;

import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;

/**
* 验证码配置属性
Expand All @@ -43,9 +38,10 @@
public class CaptchaProperties {

/**
* 图片验证码配置
* 过期时间
*/
private CaptchaImage image;
@Value("${captcha.graphic.expirationInMinutes}")
private long expirationInMinutes;

/**
* 邮箱验证码配置
Expand All @@ -57,61 +53,6 @@ public class CaptchaProperties {
*/
private CaptchaSms sms;

/**
* 图片验证码配置
*/
@Data
public static class CaptchaImage {
/**
* 类型
*/
private CaptchaImageTypeEnum type;

/**
* 内容长度
*/
private int length;

/**
* 过期时间
*/
private long expirationInMinutes;

/**
* 宽度
*/
private int width = 111;

/**
* 高度
*/
private int height = 36;

/**
* 字体
*/
private String fontName;

/**
* 字体大小
*/
private int fontSize = 25;

/**
* 获取图片验证码对象
*
* @return 验证码对象
*/
public Captcha getCaptcha() {
Captcha captcha = ReflectUtil.newInstance(type.getClazz(), this.width, this.height);
captcha.setLen(length);
if (StrUtil.isNotBlank(this.fontName)) {
captcha.setFont(new Font(this.fontName, Font.PLAIN, this.fontSize));
}
return captcha;
}
}

/**
* 邮箱验证码配置
*/
Expand Down Expand Up @@ -163,42 +104,4 @@ public static class CaptchaSms {
*/
private String templateId;
}

/**
* 图片验证码类型枚举
*/
@Getter
@RequiredArgsConstructor
private enum CaptchaImageTypeEnum {

/**
* 算术
*/
ARITHMETIC(ArithmeticCaptcha.class),

/**
* 中文
*/
CHINESE(ChineseCaptcha.class),

/**
* 中文闪图
*/
CHINESE_GIF(ChineseGifCaptcha.class),

/**
* 闪图
*/
GIF(GifCaptcha.class),

/**
* 特殊类型
*/
SPEC(SpecCaptcha.class),;

/**
* 验证码字节码类型
*/
private final Class<? extends Captcha> clazz;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import top.charles7c.cnadmin.common.util.TemplateUtils;
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
import top.charles7c.continew.starter.cache.redisson.util.RedisUtils;
import top.charles7c.continew.starter.captcha.graphic.autoconfigure.GraphicCaptchaProperties;

/**
* 验证码 API
Expand All @@ -73,17 +74,15 @@ public class CaptchaController {

private final CaptchaProperties captchaProperties;
private final ProjectProperties projectProperties;
private final GraphicCaptchaProperties graphicCaptchaProperties;

@Operation(summary = "获取图片验证码", description = "获取图片验证码(Base64编码,带图片格式:data:image/gif;base64)")
@GetMapping("/img")
public CaptchaResp getImageCaptcha() {
// 生成验证码
CaptchaProperties.CaptchaImage captchaImage = captchaProperties.getImage();
Captcha captcha = captchaImage.getCaptcha();
// 保存验证码
Captcha captcha = graphicCaptchaProperties.getCaptcha();
String uuid = IdUtil.fastUUID();
String captchaKey = RedisUtils.formatKey(CacheConstants.CAPTCHA_KEY_PREFIX, uuid);
RedisUtils.set(captchaKey, captcha.text(), Duration.ofMinutes(captchaImage.getExpirationInMinutes()));
RedisUtils.set(captchaKey, captcha.text(), Duration.ofMinutes(captchaProperties.getExpirationInMinutes()));
return CaptchaResp.builder().uuid(uuid).img(captcha.toBase64()).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,15 @@ spring.mail:

--- ### 验证码配置
captcha:
## 图片验证码配置
image:
## 图形验证码配置
graphic:
enabled: true
# 类型
type: SPEC
# 内容长度
length: 4
# 过期时间
expirationInMinutes: 2
# 宽度
width: 111
# 高度
height: 36
## 邮箱验证码配置
mail:
# 内容长度
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,15 @@ spring.mail:

--- ### 验证码配置
captcha:
## 图片验证码配置
image:
## 图形验证码配置
graphic:
enabled: true
# 类型
type: SPEC
# 内容长度
length: 4
# 过期时间
expirationInMinutes: 2
# 宽度
width: 111
# 高度
height: 36
## 邮箱验证码配置
mail:
# 内容长度
Expand Down
16 changes: 0 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
<justauth.version>1.16.5</justauth.version>
<easyexcel.version>3.3.2</easyexcel.version>
<ip2region.version>3.1.5.1</ip2region.version>
<easy-captcha.version>1.6.2</easy-captcha.version>
<hutool.version>5.8.22</hutool.version>

<!-- Maven Plugin Versions -->
<spotless.version>2.40.0</spotless.version>
Expand Down Expand Up @@ -113,20 +111,6 @@
<version>${ip2region.version}</version>
</dependency>

<!-- Easy Captcha(Java 图形验证码,支持 gif、中文、算术等类型,可用于 Java Web、JavaSE 等项目) -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>${easy-captcha.version}</version>
</dependency>

<!-- Hutool(小而全的 Java 工具类库,通过静态方法封装,降低相关 API 的学习成本,提高工作效率,使 Java 拥有函数式语言般的优雅,让 Java 语言也可以“甜甜的”) -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>

<!-- ################ 本项目子模块相关 ################ -->
<!-- API 模块(存放 Controller 层代码,打包部署的模块) -->
<dependency>
Expand Down

0 comments on commit 8a11a02

Please sign in to comment.