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 ...
随机推荐
- [转]CENTOS LINUX安装并使用NFS共享文件
FROM :http://www.qiansw.com/centos-linux-nfs.html NFS是linux常用的一种文件分享工具. 下面介绍安装及使用方法. CentOS 5.5 yum ...
- Chapter 4 -- Throwables
TODO: rewrite with more examples Guava's Throwables utility can frequently simplify dealing with exc ...
- jdk环境变量配置 java环境变量配置
进行java开发,首先要安装jdk,安装了jdk后还要进行环境变量配置: 1.下载jdk(http://java.sun.com/javase/downloads/index.jsp),我下载的版本是 ...
- USB线插拔检测使用UEventObserver检测uevent事件的分析
说实话这玩样儿的代码量真的很少,大家如果能耐得住性子啃一会儿也就能撸懂了. 在这之前研究USB线插拔的时候就知道了有这么个东西,当时也就看了看,但没做什么笔记.最近想用起来,却发现就只有个名字在记忆中 ...
- http://download.csdn.net/detail/yanzi1225627/6548337
[2013.9.8网络首发]导入Android4.2源码里的Gallery2和Camera模块至Eclipse全过程 上次导入的时候是新建的一个user library,然后把所需要的四个库文件放里面 ...
- [leetcode]Permutations II @ Python
原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...
- iOS开发-UITextView实现PlaceHolder的方式
之前开发遇到过UITextField中加入一个PlaceHolder的问题,直接设置一下即可,不过这次是需要在UITextView中实现一个PlaceHolder,跟之前有点不同.在网上参考了各位前辈 ...
- GPS精度因子(GDOP,PDOP,HDOP,VDOP,TDOP)
PDOP:位置精度因子(Position Dilution of Precision),直译为“精度强弱度”,通常翻译为“相对误差”.具体含义是:由于观测成果的好坏与被测量的人造卫星和接收仪间的几何形 ...
- set练习
#include <iostream> #include <set> #include <vector> using namespace std; int main ...
- OpenFeign封装为springboot starter
OpenFeign是什么 随着业务的增多,我们的单体应用越来越复杂,单机已经难以满足性能的需求,这时候出现了分布式.分布式通讯除了RPC, REST HTTP请求是最简单的一种方式.OpenFeign ...