自定义字符串验证码生成器
This commit is contained in:
parent
6a6d1d4c5e
commit
03e730319e
|
|
@ -72,10 +72,6 @@ public class IpUtils
|
|||
ip = request.getHeader("X-Real-IP");
|
||||
}
|
||||
|
||||
|
||||
if (ip == null || ip.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ip)){
|
||||
ip = request.getRemoteAddr();
|
||||
}
|
||||
String remoteAddr = request.getRemoteAddr();
|
||||
if (!StringUtils.isEmpty(ip) && !StringUtils.isEmpty(remoteAddr) && !ObjectUtils.isEmpty(trustedProxy)) {
|
||||
//使用代理的情况下确定代理是可信的
|
||||
|
|
|
|||
|
|
@ -32,10 +32,12 @@ public class CaptchaConfig
|
|||
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "40");
|
||||
// KAPTCHA_SESSION_KEY
|
||||
properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");
|
||||
// 验证码文本字符长度 默认为5
|
||||
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "6");
|
||||
// // 验证码文本字符长度 默认为5,这个在自定义文本生成器里定义
|
||||
// properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
|
||||
// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
|
||||
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
|
||||
|
||||
properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.bonus.gateway.config.MixedTextCreator");
|
||||
// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
|
||||
// properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
|
||||
Config config = new Config(properties);
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import java.util.Random;
|
|||
import com.google.code.kaptcha.text.impl.DefaultTextCreator;
|
||||
|
||||
/**
|
||||
* 验证码文本生成器
|
||||
* 数学计算验证码文本生成器
|
||||
*
|
||||
* @author bonus
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
package com.bonus.gateway.config;
|
||||
|
||||
|
||||
import com.google.code.kaptcha.text.TextProducer;
|
||||
import java.util.Random;
|
||||
|
||||
public class MixedTextCreator implements TextProducer {
|
||||
private static final String NUMBERS = "23456789";
|
||||
private static final String LETTERS = "abcdefghijkmnopqrstuvwxyz";
|
||||
private final Random random = new Random();
|
||||
|
||||
@Override
|
||||
public String getText() {
|
||||
// 确保至少包含2个数字和2个字母
|
||||
StringBuilder text = new StringBuilder(6);
|
||||
|
||||
// 添加2个随机数字
|
||||
for (int i = 0; i < 2; i++) {
|
||||
text.append(NUMBERS.charAt(random.nextInt(NUMBERS.length())));
|
||||
}
|
||||
|
||||
// 添加2个随机字母
|
||||
for (int i = 0; i < 2; i++) {
|
||||
text.append(LETTERS.charAt(random.nextInt(LETTERS.length())));
|
||||
}
|
||||
|
||||
// 添加剩余2个随机字符(可以是数字或字母)
|
||||
String allChars = NUMBERS + LETTERS;
|
||||
for (int i = 0; i < 2; i++) {
|
||||
text.append(allChars.charAt(random.nextInt(allChars.length())));
|
||||
}
|
||||
|
||||
// 打乱字符顺序
|
||||
char[] chars = text.toString().toCharArray();
|
||||
for (int i = chars.length - 1; i > 0; i--) {
|
||||
int index = random.nextInt(i + 1);
|
||||
char temp = chars[index];
|
||||
chars[index] = chars[i];
|
||||
chars[i] = temp;
|
||||
}
|
||||
|
||||
return new String(chars);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue