小工具——生成验证码

小工具——生成验证码

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.最后生成随机字符

操作简简单单,每天一个小工具。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值