google captcha验证码生成工具使用教程 样式配置

Posted 一只猪的思考

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了google captcha验证码生成工具使用教程 样式配置相关的知识,希望对你有一定的参考价值。

1、引入依赖

<dependency>
			<groupId>com.github.penggle</groupId>
			<artifactId>kaptcha</artifactId>
			<version>2.3.2</version>
</dependency>

2、代码解析

通过源码分析可知,captcha主要是由Producer接口创建而成

而DefaultKaptcha又实现了Producer接口

所以我们只需要创建DefaultKaptcha对象即可生成验证码

通过Configurable源码又可得知这是配置验证码的父类
于是我们可以通过查阅官网资料配置properties
再通过Configurable继承的父类setConfig方法设置验证码样式即可
官网配置方法写在了最后面
下面是操作代码,大家可以结合理解

package com.liu.community.config;

import com.google.code.kaptcha.Producer;
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.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@Configuration
public class CaptchaConfiguration

    @Bean
    public Producer captchaProducer()
        Properties properties = new Properties();
        DefaultKaptcha kaptcha =null;
        InputStream in = CaptchaConfiguration.class.getClassLoader().getResourceAsStream("captcha.properties");
        try 
            properties.load(in);
            kaptcha = new DefaultKaptcha();
            kaptcha.setConfig(new Config(properties));
         catch (IOException e) 
            e.printStackTrace();
        

        return kaptcha;
    


@RequestMapping(path = "/captcha",method = RequestMethod.GET)
    public void generateCaptcha(HttpServletResponse response,HttpSession session)
        Producer producer = captchaConfiguration.captchaProducer();

//        生成验证码
        String text = producer.createText();
//        根据验证码生成图片
        BufferedImage image = producer.createImage(text);
//        存储验证码,登陆时检验
        session.setAttribute("captcha",text);
//        生成的是png格式
        response.setContentType("image/png");
        try 
            ServletOutputStream outputStream = response.getOutputStream();
//            传输图片
            ImageIO.write(image,"png",outputStream);
         catch (IOException e) 
            logger.error("响应验证码失败:" + e.getMessage());
        


    

配置文件

 kaptcha.image.width = 100
kaptcha.image.height = 40
kaptcha.textproducer.char.string = 123456789abcdefghijklmnopqrstuvwxiABCDEFGHIJKLMNOPQRSTUVWXI
kaptcha.textproducer.char.length = 4
kaptcha.textproducer.font.size = 30
kaptcha.textproducer.font.color = black

3、编写配置类

常量描述默认
kaptcha.borderBorder around kaptcha. Legal values are yes or no.yes
kaptcha.border.colorColor of the border. Legal values are r,g,b (and optional alpha) or white,black,blue.black
kaptcha.border.thicknessThickness of the border around kaptcha. Legal values are > 0.1
kaptcha.image.widthWidth in pixels of the kaptcha image.200
kaptcha.image.heightHeight in pixels of the kaptcha image.50
kaptcha.producer.implThe image producer.com.google.code.kaptcha.impl.DefaultKaptcha
kaptcha.textproducer.implThe text producer.com.google.code.kaptcha.text.impl.DefaultTextCreator
kaptcha.textproducer.char.stringThe characters that will create the kaptcha.abcde2345678gfynmnpwx
kaptcha.textproducer.char.lengthThe number of characters to display.5
kaptcha.textproducer.font.namesA list of comma separated font names.Arial, Courier
kaptcha.textproducer.font.sizeThe size of the font to use.40px.
kaptcha.textproducer.font.colorThe color to use for the font. Legal values are r,g,b.black
kaptcha.textproducer.char.spaceThe space between the characters2
kaptcha.noise.implThe noise producer.com.google.code.kaptcha.impl.DefaultNoise
kaptcha.noise.colorThe noise color. Legal values are r,g,b.black
kaptcha.obscurificator.implThe obscurificator implementation.com.google.code.kaptcha.impl.WaterRipple
kaptcha.background.implThe background implementation.com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.fromStarting background color. Legal values are r,g,b.light grey
kaptcha.background.clear.toEnding background color. Legal values are r,g,b.white
kaptcha.word.implThe word renderer implementation.com.google.code.kaptcha.text.impl.DefaultWordRenderer
kaptcha.session.keyThe value for the kaptcha is generated and is put into the HttpSession. This is the key value for that item in the session.KAPTCHA_SESSION_KEY
kaptcha.session.dateThe date the kaptcha is generated is put into the HttpSession. This is the key value for that item in the session.KAPTCHA_SESSION_DATE

以上是关于google captcha验证码生成工具使用教程 样式配置的主要内容,如果未能解决你的问题,请参考以下文章

小工具 ——快速生成验证码

java后台验证码工具

使用 Python 生成验证码(CAPTCHA)

用PHP生成验证码

使用 Captcha 扩展包 为 Laravel 5 应用生成验证码

Laravel验证码工具gregwar/captcha