项目中引入POM:

<dependency>
<groupId>com.google.code.kaptcha</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3</version>
</dependency>

该jar在http://mvnrepository.com/artifact/com.google.code.kaptcha/kaptcha 和 https://repository.sonatype.org/#nexus-search;quick~com.google.code.kaptcha 可以下载到。

kaptcha提供了KaptchaServlet来处理验证码的生成并放入session,但这方式在如今的分布式、前后端分离、集群的部署条件显然不适用。

kaptcha生成验证的核心接口类有:

1、com.google.code.kaptcha.Producer,该接口目前只有一个默认实现类:com.google.code.kaptcha.impl.DefaultKaptcha

2、com.google.code.kaptcha.text.TextProducer,该忌口目前只有一个默认实现类:com.google.code.kaptcha.text.impl.DefaultTextCreator

其中Producer是用来处理生成图片的,TextProducer是用来处理生成验证码问题的。

当然我们也可以去实现这个接口来实现自己的自定义的实现。

控制验证码的图片的生成的规则的配置信息都放到了com.google.code.kaptcha.util.Config类中,其源码如下:

 package com.google.code.kaptcha.util;

 import java.awt.Color;
import java.awt.Font;
import java.util.Properties; import com.google.code.kaptcha.BackgroundProducer;
import com.google.code.kaptcha.GimpyEngine;
import com.google.code.kaptcha.NoiseProducer;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultBackground;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.impl.DefaultNoise;
import com.google.code.kaptcha.impl.WaterRipple;
import com.google.code.kaptcha.text.TextProducer;
import com.google.code.kaptcha.text.WordRenderer;
import com.google.code.kaptcha.text.impl.DefaultTextCreator;
import com.google.code.kaptcha.text.impl.DefaultWordRenderer;
import com.google.code.kaptcha.util.ConfigHelper; public class Config
{
private Properties properties;
private ConfigHelper helper; public Config(Properties properties)
{
this.properties = properties;
this.helper = new ConfigHelper();
} /**
* 设置图片是否有边框
* @return
*/
public boolean isBorderDrawn()
{
String paramName = "kaptcha.border";
String paramValue = this.properties.getProperty(paramName);
return this.helper.getBoolean(paramName, paramValue, true);
} /**
* 边框颜色 合法值: r,g,b (and optional alpha) 或者 white,black,blue.
* @return
*/
public Color getBorderColor()
{
String paramName = "kaptcha.border.color";
String paramValue = this.properties.getProperty(paramName);
return this.helper.getColor(paramName, paramValue, Color.BLACK);
} /**
* 边框厚度 合法值:>0
* @return
*/
public int getBorderThickness()
{
String paramName = "kaptcha.border.thickness";
String paramValue = this.properties.getProperty(paramName);
return this.helper.getPositiveInt(paramName, paramValue, 1);
} /**
* 文本集合,验证码值从此集合中获取
* @return
*/
public char[] getTextProducerCharString()
{
String paramName = "kaptcha.textproducer.char.string";
String paramValue = this.properties.getProperty(paramName);
return this.helper.getChars(paramName, paramValue, "abcde2345678gfynmnpwx".toCharArray());
} /**
* 验证码长度
* @return
*/
public int getTextProducerCharLength()
{
String paramName = "kaptcha.textproducer.char.length";
String paramValue = this.properties.getProperty(paramName);
return this.helper.getPositiveInt(paramName, paramValue, 5);
} /**
* 字体类型
* @param fontSize 见Font中的定义
* @return
*/
public Font[] getTextProducerFonts(int fontSize)
{
String paramName = "kaptcha.textproducer.font.names";
String paramValue = this.properties.getProperty(paramName);
return this.helper.getFonts(paramName, paramValue, fontSize, new Font[] { new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize) });
} /**
* 字体大小
* @return
*/
public int getTextProducerFontSize()
{
String paramName = "kaptcha.textproducer.font.size";
String paramValue = this.properties.getProperty(paramName);
return this.helper.getPositiveInt(paramName, paramValue, 40);
} /**
* 字体颜色 rgb颜色或者Color中的值
* @return
*/
public Color getTextProducerFontColor()
{
String paramName = "kaptcha.textproducer.font.color";
String paramValue = this.properties.getProperty(paramName);
return this.helper.getColor(paramName, paramValue, Color.BLACK);
} /**
* 干扰线的颜色
* @return
*/
public Color getNoiseColor()
{
String paramName = "kaptcha.noise.color";
String paramValue = this.properties.getProperty(paramName);
return this.helper.getColor(paramName, paramValue, Color.BLACK);
} /**
* 背景颜色渐变色开始色 rgb或者Color中定义的
* @return
*/
public Color getBackgroundColorFrom()
{
String paramName = "kaptcha.background.clear.from";
String paramValue = this.properties.getProperty(paramName);
return this.helper.getColor(paramName, paramValue, Color.LIGHT_GRAY);
} /**
* 背景颜色渐变色结束色 rgb或者Color中定义的
* @return
*/
public Color getBackgroundColorTo()
{
String paramName = "kaptcha.background.clear.to";
String paramValue = this.properties.getProperty(paramName);
return this.helper.getColor(paramName, paramValue, Color.WHITE);
} /**
* 图片的宽度
* @return
*/
public int getWidth()
{
String paramName = "kaptcha.image.width";
String paramValue = this.properties.getProperty(paramName);
return this.helper.getPositiveInt(paramName, paramValue, 200);
} /**
* 图片的高度
* @return
*/
public int getHeight()
{
String paramName = "kaptcha.image.height";
String paramValue = this.properties.getProperty(paramName);
return this.helper.getPositiveInt(paramName, paramValue, 50);
} /**
* 图片的session key
* @return
*/
public String getSessionKey()
{
return this.properties.getProperty("kaptcha.session.key", "KAPTCHA_SESSION_KEY");
} public Properties getProperties()
{
return this.properties;
} /**
* 生成默认的图片生产者实现
* @return
*/
public Producer getProducerImpl()
{
String paramName = "kaptcha.producer.impl";
String paramValue = this.properties.getProperty(paramName);
Producer producer = (Producer)this.helper.getClassInstance(paramName, paramValue, new DefaultKaptcha(), this);
return producer;
} /**
* 生成默认的验证码文字生产者实现
* @return
*/
public TextProducer getTextProducerImpl()
{
String paramName = "kaptcha.textproducer.impl";
String paramValue = this.properties.getProperty(paramName);
TextProducer textProducer = (TextProducer)this.helper.getClassInstance(paramName, paramValue, new DefaultTextCreator(), this); return textProducer;
} /**
* 文字干扰实现类,默认DefaultNoise,还可以选择com.google.code.kaptcha.impl.NoNoise没有干扰线的实现类
* @return
*/
public NoiseProducer getNoiseImpl()
{
String paramName = "kaptcha.noise.impl";
String paramValue = this.properties.getProperty(paramName);
NoiseProducer noiseProducer = (NoiseProducer)this.helper.getClassInstance(paramName, paramValue, new DefaultNoise(), this); return noiseProducer;
} /**
* 图片样式的实现类,默认WaterRipple(水纹),还有下面2种可选
* 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
*
* @return
*/
public GimpyEngine getObscurificatorImpl()
{
String paramName = "kaptcha.obscurificator.impl";
String paramValue = this.properties.getProperty(paramName);
GimpyEngine gimpyEngine = (GimpyEngine)this.helper.getClassInstance(paramName, paramValue, new WaterRipple(), this);
return gimpyEngine;
} /**
* 文字渲染实现类,默认DefaultWordRenderer,也只有这一个默认的实现类
* @return
*/
public WordRenderer getWordRendererImpl()
{
String paramName = "kaptcha.word.impl";
String paramValue = this.properties.getProperty(paramName);
WordRenderer wordRenderer = (WordRenderer)this.helper.getClassInstance(paramName, paramValue, new DefaultWordRenderer(), this); return wordRenderer;
} /**
* 背景图片实现类,默认DefaultBackground,也只有这一个默认实现类
* @return
*/
public BackgroundProducer getBackgroundImpl()
{
String paramName = "kaptcha.background.impl";
String paramValue = this.properties.getProperty(paramName);
BackgroundProducer backgroundProducer = (BackgroundProducer)this.helper.getClassInstance(paramName, paramValue, new DefaultBackground(), this); return backgroundProducer;
}
}

看config的源码就知道可以配置哪些属性了。

下面是spring boot的一个装配的实例代码:

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config; @Configuration
public class KaptchaConfig { @Bean
public Producer KaptchaProducer() {
Properties kaptchaProperties = new Properties();
kaptchaProperties.put("kaptcha.border", "no");
kaptchaProperties.put("kaptcha.textproducer.char.length","4");
kaptchaProperties.put("kaptcha.image.height","50");
kaptchaProperties.put("kaptcha.image.width","150");
kaptchaProperties.put("kaptcha.obscurificator.impl","com.google.code.kaptcha.impl.ShadowGimpy");
kaptchaProperties.put("kaptcha.textproducer.font.color","black");
kaptchaProperties.put("kaptcha.textproducer.font.size","40");
kaptchaProperties.put("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");
//kaptchaProperties.put("kaptcha.noise.impl","com.google.code.kaptcha.impl.DefaultNoise");
kaptchaProperties.put("kaptcha.textproducer.char.string","acdefhkmnprtwxy2345678"); Config config = new Config(kaptchaProperties);
return config.getProducerImpl();
}
}

使用的话常用的方法有:

package com.google.code.kaptcha;

import java.awt.image.BufferedImage;

public abstract interface Producer
{
public abstract BufferedImage createImage(String kaptchaText); public abstract String createText();
}

得到BufferedImage可以写base64的格式的图片给前端,例如:

String kaptchaText = kaptchaProducer.createText();
log.info("capText = {} ,kaptchaKey = {} ", kaptchaText, kaptchaKey);
BufferedImage bi = kaptchaProducer.createImage(kaptchaText);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(bi, "jpeg", outputStream);
String base64Image = "data:image/jpeg;base64," + Base64.encodeBase64String(outputStream.toByteArray());

SpringBoot之配置google kaptcha的更多相关文章

  1. Springboot +redis+⾕歌开源Kaptcha实现图片验证码功能

    Springboot +redis+⾕歌开源Kaptcha实现图片验证码功能 背景 注册-登录-修改密码⼀般需要发送验证码,但是容易被 攻击恶意调⽤ 什么是短信-邮箱轰炸机 手机短信轰炸机是批.循环给 ...

  2. Spring MVC 中使用 Google kaptcha 验证码

    验证码是抵抗批量操作和恶意登录最有效的方式之一. 验证码从产生到现在已经衍生出了很多分支.方式.google kaptcha 是一个非常实用的验证码生成类库. 通过灵活的配置生成各种样式的验证码,并将 ...

  3. Google Kaptcha验证码的使用

    原文:http://www.kailing.pub/article/index/arcid/92.html Kaptcha是什么? kaptcha 是谷歌开源的非常实用的验证码生成工具,基于Simpl ...

  4. SpringBoot常用配置简介

    SpringBoot常用配置简介 1. SpringBoot中几个常用的配置的简单介绍 一个简单的Spring.factories # Bootstrap components org.springf ...

  5. 在SpringBoot中配置aop

    前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...

  6. SpringBoot cache-control 配置静态资源缓存 (以及其中的思考经历)

    昨天在部署项目时遇到一个问题,因为服务要部署到外网使用,中间经过了较多的网络传输限制,而且要加载arcgis等较大的文件,所以在部署后,发现页面loading需要很长时间,而且刷新也要重新从服务器下载 ...

  7. 补习系列(10)-springboot 之配置读取

    目录 简介 一.配置样例 二.如何注入配置 1. 缺省配置文件 2. 使用注解 3. 启动参数 还有.. 三.如何读取配置 @Value 注解 Environment 接口 @Configuratio ...

  8. springboot +redis配置

    springboot +redis配置 pom依赖 <dependency> <groupId>org.springframework.boot</groupId> ...

  9. SpringBoot之配置

    回顾 SpringBoot之基础 配置文件 ① 两种全局配置文件(文件名是固定的) 配置文件放在src/main/resources目录或者类路径/config下 application.proper ...

随机推荐

  1. [Web 前端] td长度固定,内容过长,超过部分用省略号代替

    cp from : https://blog.csdn.net/bsh_csn/article/details/51829103 html的table表格中td长度固定,当内容过长时,超过部分用省略号 ...

  2. Forward secrecy

    In cryptography, forward secrecy (FS), also known as perfect forward secrecy (PFS), is a property of ...

  3. PHP Curl transfer closed with outstanding read data remaining

    php transfer closed with outstanding read data remaining php curl CURLOPT_HTTPHEADER php curl 报错 tra ...

  4. Verilog 加法器和减法器(3)

    手工加法运算时候,我们都是从最低位的数字开始,逐位相加,直到最高位.如果第i位产生进位,就把该位作为第i+1位输入.同样的,在逻辑电路中,我们可以把一位全加器串联起来,实现多位加法,比如下面的四位加法 ...

  5. matlib实现梯度下降法(序一)

    数据来源:http://archive.ics.uci.edu/ml/datasets/Combined+Cycle+Power+Plant 数据描述: 有四个输入特征,这些数据来自电厂,这四个特征和 ...

  6. 【转】asm.js 和 Emscripten 入门教程

      Web 技术突飞猛进,但是有一个领域一直无法突破 ---- 游戏. 游戏的性能要求非常高,一些大型游戏连 PC 跑起来都很吃力,更不要提在浏览器的沙盒模型里跑了!但是,尽管很困难,许多开发者始终没 ...

  7. go语言之进阶篇通过map生成json

    1.通过map生成json 示例1: package main import ( "encoding/json" "fmt" ) func main() { / ...

  8. 关于html+ashx开发中几个问题的解决方法 (转)

    在跟html+ashx打交道的园友们肯定会发现,这种模式虽然优美,但在开发中会遇到一些难处理的地方.我也不例外,下面是自己在实际开发中总结出来的几条经验,希望跟大家分享,更希望得到大家的建议和更好的解 ...

  9. Hadoop视频教程汇总

    一 慕课网 1.Hadoop大数据平台架构与实践--基础篇(已学习) 链接:https://www.imooc.com/learn/391 2.Hadoop进阶(已学习) 链接:https://www ...

  10. 使用JDBC向Kudu表插入中文数据乱码(转载)

    参考:https://cloud.tencent.com/developer/article/1077763 问题描述 使用Impala JDBC向Kudu表中插入中文字符,插入的中文字符串乱码,中文 ...