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 ...
随机推荐
- 华硕 RT-AC54U路由器固件功能说明
引言 华硕 RT-AC54U这款路由器固件,界面做的非常不错(起码比OpenWrt要好看).功能也比較强悍,可是对于刚入手这个固件的用户可能会对此固件的一些非常好用的功能无从下手,所以这里我就写下了这 ...
- Spring框架中IoC(控制反转)的原理(转)
原文链接:Spring框架中IoC(控制反转)的原理 一.IoC的基础知识以及原理: 1.IoC理论的背景:在采用面向对象方法设计的软件系统中,底层实现都是由N个对象组成的,所有的对象通过彼此的合作, ...
- JavaScript:Array 对象
ylbtech-JavaScript:Array 对象 1. 返回顶部 Array 对象 Array 对象用于在单个的变量中存储多个值. 创建 Array 对象的语法: new Array(); ne ...
- Valid Palindrome leetcode java
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- jquery的$.extend和$.fn.extend作用及区别,兼它们的一些小细节
$.extend(obj);是为了扩展jquery本身,为类添加新的方法 $.fn.extend(obj);给JQUERY对象添加方法.如(1): $.extend({ add:function( ...
- SQL操作查漏补缺
SQL教程地址:http://www.w3school.com.cn/sql/index.asp TOP 子句 TOP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是 ...
- php7安装mongoDB扩展
本文我们使用pecl命令来安装 首先来到php7的安装目录 $ /usr/local/php7/bin/pecl install mongodb 回车,执行成功后,会输出以下结果: …… Build ...
- 【Scala】Java-Scala-单例模式实现方法
Java-Scala-单例模式实现方法 scala 单例_百度搜索 scala实现单例模式-博客-云栖社区-阿里云
- POJ 1265 pick定理
pick公式:多边形的面积=多边形边上的格点数目/2+多边形内部的格点数目-1. 多边形边上的格点数目可以枚举每条边求出.如果是水平或者垂直,显然可以得到,否则则是坐标差的最大公约数减1.(注这里是不 ...
- Java归去来第1集:手动给Eclipse配置Maven环境
一.Eclipse配置Maven 1.1.下载Maven http://maven.apache.org/download.cgi,选择对应的版本,window下载apache-maven-3.5.3 ...