9 changed files with 152 additions and 108 deletions
@ -0,0 +1,27 @@ |
|||
package com.dongjian.datacenter.admin.service.captcha; |
|||
|
|||
import cn.hutool.captcha.generator.CodeGenerator; |
|||
|
|||
public class HutoolCaptchaGenerator implements CodeGenerator { |
|||
|
|||
private int length = 4; |
|||
private static final String chars = "23456789abcdefghkmnpqrstuvwxyzABCDEFGHKMNPRSTUVWXYZ"; |
|||
|
|||
@Override |
|||
public String generate() { |
|||
StringBuilder sb = new StringBuilder(length); |
|||
for (int i = 0; i < length; i++) { |
|||
int idx = (int) (Math.random() * chars.length()); |
|||
sb.append(chars.charAt(idx)); |
|||
} |
|||
return sb.toString(); |
|||
} |
|||
|
|||
@Override |
|||
public boolean verify(String code, String userInput) { |
|||
if (code == null || userInput == null) { |
|||
return false; |
|||
} |
|||
return code.equalsIgnoreCase(userInput); |
|||
} |
|||
} |
|||
@ -1,36 +0,0 @@ |
|||
package com.dongjian.datacenter.admin.service.captcha; |
|||
|
|||
import java.util.Properties; |
|||
|
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
|
|||
import com.google.code.kaptcha.impl.DefaultKaptcha; |
|||
import com.google.code.kaptcha.util.Config; |
|||
|
|||
@Configuration |
|||
public class KaptchaConfig { |
|||
@Bean |
|||
public DefaultKaptcha producer(){ |
|||
|
|||
DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); |
|||
Properties properties = new Properties(); |
|||
properties.setProperty("kaptcha.border", "no"); |
|||
properties.setProperty("kaptcha.border.color", "105,179,90"); |
|||
properties.setProperty("kaptcha.textproducer.font.color", "black"); |
|||
properties.setProperty("kaptcha.image.width", "110"); |
|||
properties.setProperty("kaptcha.image.height", "40"); |
|||
properties.setProperty("kaptcha.textproducer.char.string","23456789abcdefghkmnpqrstuvwxyzABCDEFGHKMNPRSTUVWXYZ"); |
|||
properties.setProperty("kaptcha.textproducer.font.size", "30"); |
|||
properties.setProperty("kaptcha.textproducer.char.space","3"); |
|||
properties.setProperty("kaptcha.session.key", "code"); |
|||
properties.setProperty("kaptcha.textproducer.char.length", "4"); |
|||
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑"); |
|||
// properties.setProperty("kaptcha.obscurificator.impl","com.xxx");可以重写实现类
|
|||
properties.setProperty("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise"); |
|||
Config config = new Config(properties); |
|||
defaultKaptcha.setConfig(config); |
|||
|
|||
return defaultKaptcha; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue