SpringBoot之配置google kaptcha
项目中引入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的更多相关文章
- Springboot +redis+⾕歌开源Kaptcha实现图片验证码功能
Springboot +redis+⾕歌开源Kaptcha实现图片验证码功能 背景 注册-登录-修改密码⼀般需要发送验证码,但是容易被 攻击恶意调⽤ 什么是短信-邮箱轰炸机 手机短信轰炸机是批.循环给 ...
- Spring MVC 中使用 Google kaptcha 验证码
验证码是抵抗批量操作和恶意登录最有效的方式之一. 验证码从产生到现在已经衍生出了很多分支.方式.google kaptcha 是一个非常实用的验证码生成类库. 通过灵活的配置生成各种样式的验证码,并将 ...
- Google Kaptcha验证码的使用
原文:http://www.kailing.pub/article/index/arcid/92.html Kaptcha是什么? kaptcha 是谷歌开源的非常实用的验证码生成工具,基于Simpl ...
- SpringBoot常用配置简介
SpringBoot常用配置简介 1. SpringBoot中几个常用的配置的简单介绍 一个简单的Spring.factories # Bootstrap components org.springf ...
- 在SpringBoot中配置aop
前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...
- SpringBoot cache-control 配置静态资源缓存 (以及其中的思考经历)
昨天在部署项目时遇到一个问题,因为服务要部署到外网使用,中间经过了较多的网络传输限制,而且要加载arcgis等较大的文件,所以在部署后,发现页面loading需要很长时间,而且刷新也要重新从服务器下载 ...
- 补习系列(10)-springboot 之配置读取
目录 简介 一.配置样例 二.如何注入配置 1. 缺省配置文件 2. 使用注解 3. 启动参数 还有.. 三.如何读取配置 @Value 注解 Environment 接口 @Configuratio ...
- springboot +redis配置
springboot +redis配置 pom依赖 <dependency> <groupId>org.springframework.boot</groupId> ...
- SpringBoot之配置
回顾 SpringBoot之基础 配置文件 ① 两种全局配置文件(文件名是固定的) 配置文件放在src/main/resources目录或者类路径/config下 application.proper ...
随机推荐
- serializeArray()与 serialize()
serialize()序列表表格内容为字符串,用于 Ajax 请求. serializeArray()序列化表格元素 (类似 '.serialize()' 方法) 返回 JSON 数据结构数据. .s ...
- [转]Linux awk 命令 说明
From : http://blog.csdn.net/tianlesoftware/article/details/6278273 一. AWK 说明 awk是一种编程语言,用于在linux/un ...
- Java学习笔记——File类文件管理及IO读写、复制操作
File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图片 文件和文件夹 相关函数 (boolean) mkdir( ...
- CSS 中的字体兼容写法:用CSS为英文和中文字体分别设置不同的字体
font-family的调用方法: font-family:Arial,'Times New Roman','Microsoft YaHei',SimHei; font:bold 12px/.75em ...
- vue嵌套路由总结
嵌套路由就是在一个被路由过来的页面下可以继续使用路由,嵌套也就是路由中的路由的意思. 比如在vue中,我们如果不使用嵌套路由,那么只有一个<router-view>,但是如果使用,那么在一 ...
- Java-JUC(十一):线程8锁
题目: 判断以下8种情况,输出的内容 题目一:一个Number实例对象number,两个非静态同步方法getOne,getTwo,两个线程打印输出(一个线程调用number.getOne,另外一个线程 ...
- 'Lock wait timeout exceeded; try restarting transaction'问题
OperationalError: (1205, 'Lock wait timeout exceeded; try restarting transaction') 原因很简单,太多错误,意外处理没有 ...
- 如何启动docker service
From powershell prompt following works for me with no issues restart-service *docker* [注意] 我试了一下,这个命 ...
- IIS发布网站遇到 编译器错误消息: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary 编
编译错误: 说明:在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息:CS0016: 未能写入输出文件“c:\Windows\Microso ...
- struts笔记
Struts视频笔记: Struts是一个开源的web框架,框架提高了程序的规范的同时也约束了程序员的自由 为什么会有struts: 因为我们队mvc理解的不同,可能造成不同公司写程序的时候,规范不统 ...