温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot配置google kaptcha验证码图片生成工具

发布时间:2021-07-02 17:24:20 来源:亿速云 阅读:310 作者:chen 栏目:大数据

这篇文章主要讲解了“SpringBoot配置google kaptcha验证码图片生成工具”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot配置google kaptcha验证码图片生成工具”吧!

1、项目中引入POM:
<dependency>
	<groupId>com.github.axet</groupId>
	<artifactId>kaptcha</artifactId>
	<version>0.0.9</version>
</dependency>
2、配置参数
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;

@Configuration
public class KaptchaConfig {
/*
      Google Captcha 可配置项
     1 kaptcha.border  是否有边框  默认为true  我们可以自己设置yes,no
     2 kaptcha.border.color   边框颜色   默认为Color.BLACK
     3 kaptcha.border.thickness  边框粗细度  默认为1
     4 kaptcha.producer.impl   验证码生成器  默认为DefaultKaptcha
     5 kaptcha.textproducer.impl   验证码文本生成器  默认为DefaultTextCreator
     6 kaptcha.textproducer.char.string   验证码文本字符内容范围  默认为abcde2345678gfynmnpwx
     7 kaptcha.textproducer.char.length   验证码文本字符长度  默认为5
     8 kaptcha.textproducer.font.names    验证码文本字体样式  默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
     9 kaptcha.textproducer.font.size   验证码文本字符大小  默认为40
    10 kaptcha.textproducer.font.color  验证码文本字符颜色  默认为Color.BLACK
    11 kaptcha.textproducer.char.space  验证码文本字符间距  默认为2
    12 kaptcha.noise.impl    验证码噪点生成对象  默认为DefaultNoise
    13 kaptcha.noise.color   验证码噪点颜色   默认为Color.BLACK
    14 kaptcha.obscurificator.impl   验证码样式引擎  默认为WaterRipple
    15 kaptcha.word.impl   验证码文本字符渲染   默认为DefaultWordRenderer
    16 kaptcha.background.impl   验证码背景生成器   默认为DefaultBackground
    17 kaptcha.background.clear.from   验证码背景颜色渐进   默认为Color.LIGHT_GRAY
    18 kaptcha.background.clear.to   验证码背景颜色渐进   默认为Color.WHITE
    19 kaptcha.image.width   验证码图片宽度  默认为200
    20 kaptcha.image.height  验证码图片高度  默认为50
*/

    @Bean
    public DefaultKaptcha producer() {
        Properties properties = new Properties();
        // 是否有边框 可选yes 或者 no
        properties.put("kaptcha.border", "no");
        // 验证码文本字符颜色
        properties.put("kaptcha.textproducer.font.color", "black");
        // 验证码文本字符间距
        properties.put("kaptcha.textproducer.char.space", "5");
        // 验证码文本字体样式
        properties.put("kaptcha.textproducer.font.names", "Arial,Courier,cmr10,宋体,楷体,微软雅黑");
        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}
3、进行测试
    @Autowired
    private Producer producer;

    @Test
    public void getCaptcha() {

        //生成文字验证码
        String code = producer.createText();
        System.out.println(code); // cnxg2

        // 生成图片
        BufferedImage image = producer.createImage(code);

        try {
            // 存储到本地方便观察效果
            OutputStream out = new FileOutputStream(new File("D:/2.jpg"));
            ImageIO.write(image, "jpg", out); //将图片写入指定文件
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
4、效果图

SpringBoot配置google kaptcha验证码图片生成工具

可以看到效果是非常不错的!

感谢各位的阅读,以上就是“SpringBoot配置google kaptcha验证码图片生成工具”的内容了,经过本文的学习后,相信大家对SpringBoot配置google kaptcha验证码图片生成工具这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI