小工具——生成验证码
1.导入jar包
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2.编写Kapatch配置类
配置类加@Configuration,方法加 @Bean让spring管理。
用的的核心接口是Producer ,默认的实现类是DefaultKaptcha (感兴趣的小伙伴可以进去看看),new DefaultKaptcha() 需要一些配置参数,封装到new Config()对象,new Config()对象又依赖于new Properties();(这里注意下:Properties 存的就是key-value,就是map。)各种配置参数根据实际需要自己上网查,就不一 一实现了。 properties.setProperty(“kaptcha.image.width”, “100”);最后return new DefaultKaptcha()。配置类搞定。
@Configuration
public class KaptchaConfig {
@Bean
public Producer kaptchaProducer() {
Properties properties = new Properties();
//配置参数 网上搜。
properties.setProperty("kaptcha.image.width", "100");
properties.setProperty("kaptcha.image.height", "40");
properties.setProperty("kaptcha.textproducer.font.size", "32");
properties.setProperty("kaptcha.textproducer.font.color", "0,0,0");
properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYAZ");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
DefaultKaptcha kaptcha = new DefaultKaptcha();//需要传入一些配置参数(颜色,大小),封装到config对象
Config config = new Config(properties);//存的都是key-value,就是map。 依赖与propertie对象
kaptcha.setConfig(config);
return kaptcha;
}
}
3.最后生成随机字符
操作简简单单,每天一个小工具。