项目中引入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. Java NIO Test Case

    package org.zwl.test.nio; import java.io.IOException; import java.net.InetSocketAddress; import java ...

  2. 整理:FPGA选型

    针对性整理下FPGA选型问题 一.获取芯片资料: 要做芯片的选型,首先就是要对有可能要面对的芯片有整体的了解,也就是说要尽可能多的先获取芯片的资料.现在FPGA主要有4个生产厂家,ALTERA,XIL ...

  3. bat 批处理切换到当前脚本所在文件夹

    bat 批处理切换到当前脚本所在文件夹   切换到当前脚本所在的文件夹 ? 1 cd  %~dp0 另外附上一些bat基本内容 —————————————————————————————— 批处理常用 ...

  4. PPPOE数据包转换及SharpPcap应用

    在最近写的一个程序中需要用到Sniffer功能,但由于通过.net自身的Socket做出来的Sniffer不能达到实际应用的要求(如不能监听WIFI数据包)所以找到了WinPCAP的.NET库Shar ...

  5. easyloader分析与使用

    转载自:http://www.cnblogs.com/haogj/archive/2013/04/22/3036685.html 使用脚本库总要加载一大堆的样式表和脚本文件,在 easyui 中,除了 ...

  6. Search Insert Position leetcode java

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  7. RxJava【过滤】操作符 filter distinct throttle take skip first MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. 门罗币(MONERO)钱包生成教程

    一.下载钱包 钱包下载地址:https://getmonero.org/downloads/(如果下载缓慢请使用下载工具下载) 二.图形界面钱包生成 解压运行monero-wallet-gui.exe ...

  9. 解剖android中的闹钟app 一

    首先,看一看android市场上有哪些主流的闹钟app了,我们来进行一个简单的评测: 一.正点闹钟 这是一款源自金山技术的闹钟app,其主力创始团队都是来自于金山,其装机量,下载量都是排名第一.老样子 ...

  10. 【Spark】SparkStreaming-输出到Kafka

    SparkStreaming-输出到Kafka sparkstreaming output kafka_百度搜索 SparkStreaming采用直连方式(Direct Approach)获取Kafk ...