最近自己做项目时,有做手机号短信验证码登录,于是总结下。
配置文件
这是验证码的配置
verification-code:
# 验证码类型 math 数组计算 char 字符
captchaType: math
验证码相关代码
KaptchaTextCreator.java
/**
* 验证码文本生成器
*/
public class KaptchaTextCreator extends DefaultTextCreator {
private static final String[] CNUMBERS = "0,1,2,3,4,5,6,7,8,9,10".split(",");
@Override
public String getText() {
Integer result = 0;
Random random = new SecureRandom();
int x = random.nextInt(10);
int y = random.nextInt(10);
StringBuilder suChinese = new StringBuilder();
SecureRandom rm = null;
try {
rm = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
int randomoperands = 0;
if (null != rm) {
randomoperands = (int) Math.round(rm.nextDouble() * 2);
}
if (randomoperands == 0) {
result = x * y;
suChinese.append(CNUMBERS[x]);
suChinese.append("*");
suChinese.append(CNUMBERS[y]);
} else if (randomoperands == 1) {
if (!(x == 0) && y % x == 0) {
result = y / x;
suChinese.append(CNUMBERS[y]);
suChinese.append("/");
suChinese.append(CNUMBERS[x]);
} else {
result = x + y;
suChinese.append(CNUMBERS[x]);
suChinese.append("+");
suChinese.append(CNUMBERS[y]);
}
} else if (randomoperands == 2) {
if (x >= y) {
result = x - y;
suChinese.append(CNUMBERS[x]);

本文介绍了如何在Java项目中配置和实现短信验证码与数学型图形验证码,用于用户登录验证。通过自定义`KaptchaTextCreator`生成数学计算式的验证码,并在`CaptchaConfig`中配置验证码的显示属性。在`SysCaptchaController`中处理验证码的生成与校验,同时展示了发送手机验证码的逻辑。整个流程覆盖了验证码的生成、存储、校验以及与手机验证码的结合使用。
最低0.47元/天 解锁文章
3938

被折叠的 条评论
为什么被折叠?



