com.springframework.boxes.cookie.starter.captcha.BoxesCaptchaAutoConfiguration Maven / Gradle / Ivy
package com.springframework.boxes.cookie.starter.captcha;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
@Configuration
@EnableConfigurationProperties(BoxesCaptchaProperties.class)
@ConditionalOnProperty(value = "spring.boxes.captcha.enabled", havingValue = "true", matchIfMissing = true)
public class BoxesCaptchaAutoConfiguration {
@Autowired
private BoxesCaptchaProperties boxesCaptchaProperties;
@Bean
@ConditionalOnProperty(value = "spring.boxes.captcha.enabled", havingValue = "true", matchIfMissing = true)
public DefaultKaptcha captchaProducer() {
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
Map meta = Optional.ofNullable(boxesCaptchaProperties.getCaptchaMeta()).orElse(Maps.newHashMap());
if(MapUtils.isEmpty(meta)){
meta.put("kaptcha.border", "yes");
meta.put("kaptcha.border.color", "193,193,193");
meta.put("kaptcha.textproducer.font.color", "black");
meta.put("kaptcha.image.width", "125");
meta.put("kaptcha.image.height", "45");
meta.put("kaptcha.session.key", "code");
meta.put("kaptcha.textproducer.char.length", "4");
meta.put("kaptcha.textproducer.char.space", "3");
meta.put("kaptcha.textproducer.font.names", "彩云,宋体,楷体,微软雅黑");
}
meta.forEach((k, v)-> {
properties.setProperty(k, v);
});
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
@Bean
@ConditionalOnProperty(value = "spring.boxes.captcha.enabled", havingValue = "true", matchIfMissing = true)
public BoxesCaptchaServices boxesCaptchaProperties() {
return new BoxesCaptchaServices(
boxesCaptchaProperties.getCookieName(), boxesCaptchaProperties.getRequestName(),
captchaProducer());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy