提交 6835f2e4 编写于 作者: 如梦技术's avatar 如梦技术 🐛

🔖 预发布 v1.1.0

上级 9eb82a1e
......@@ -6,6 +6,7 @@
- :mute: nacos 日志影响了gateway 和 webflux 的日志,调高级别。
- :zap: 优化 Servlet 和 Webflux 请求日志打印效果。
- :sparkles: 添加部分工具类。
- :sparkles: 优化验证码生成。
- :sparkles: 开源所有 `mica-pro` 功能。
- :sparkles: `mica-pro` 中的 `http-cache` 注解部分移入 `mica-boot`(暂时只支持 Servlet)。
- :sparkles: 开源 `mica-cloud` 模块。
......
......@@ -84,25 +84,42 @@ public abstract class BaseCaptcha {
* @return {String}
*/
public Captcha generateBase64() {
return generate(true);
}
/**
* 生成验证码
*
* @return {String}
*/
public Captcha generate() {
return generate(false);
}
private Captcha generate(boolean isBase64) {
ThreadLocalRandom random = ThreadLocalRandom.current();
// 转成大写重要
String captchaCode = CaptchaUtils.generateCode(random).toUpperCase();
// 生成验证码
byte[] imgBytes = CaptchaUtils.generate(random, captchaCode);
String base64 = Base64Util.encodeToString(imgBytes);
String uuid = StringUtil.getUUID();
// 保存验证码缓存
captchaCache.put(uuid, captchaCode);
return new Captcha(uuid, base64);
if (isBase64) {
return new Captcha(uuid, Base64Util.encodeToString(imgBytes));
} else {
return new Captcha(uuid, imgBytes);
}
}
/**
* 校验 Base64 验证码
* 校验验证码
*
* @param uuid uuid
* @param userInputCaptcha 用户输入的验证码
* @return 是否成功
*/
public boolean validateBase64(String uuid, String userInputCaptcha) {
public boolean validate(String uuid, String userInputCaptcha) {
if (log.isInfoEnabled()) {
log.info("validate captcha userInputCaptcha is {}", userInputCaptcha);
}
......
......@@ -18,8 +18,9 @@ package net.dreamlu.mica.captcha;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.lang.Nullable;
import java.io.Serializable;
......@@ -30,7 +31,6 @@ import java.io.Serializable;
*/
@Getter
@ApiModel("验证码模型")
@AllArgsConstructor
public class Captcha implements Serializable {
/**
* 用于传给前端,校验时携带
......@@ -40,6 +40,25 @@ public class Captcha implements Serializable {
/**
* 图片 base64
*/
@Setter
@Nullable
@ApiModelProperty("验证码图片(base64)")
private final String base64;
private String base64;
/**
* 图片 bytes
*/
@Setter
@Nullable
@ApiModelProperty("验证码图片(bytes)")
private byte[] bytes;
Captcha(String uuid, String base64) {
this.uuid = uuid;
this.base64 = base64;
}
Captcha(String uuid, byte[] bytes) {
this.uuid = uuid;
this.bytes = bytes;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册