SpringBoot之配置google kaptcha

摘要:
版本>2.3<1packagecom.google.code.kaptcha.util;19 importcom.google.code.kaptcha.util.ConfigHelper;2526publicConfig(属性属性)27{28this.properties=属性;

项目中引入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类中,其源码如下:

  1 package com.google.code.kaptcha.util;
  2 
  3 import java.awt.Color;
  4 import java.awt.Font;
  5 import java.util.Properties;
  6 
  7 import com.google.code.kaptcha.BackgroundProducer;
  8 import com.google.code.kaptcha.GimpyEngine;
  9 import com.google.code.kaptcha.NoiseProducer;
 10 import com.google.code.kaptcha.Producer;
 11 import com.google.code.kaptcha.impl.DefaultBackground;
 12 import com.google.code.kaptcha.impl.DefaultKaptcha;
 13 import com.google.code.kaptcha.impl.DefaultNoise;
 14 import com.google.code.kaptcha.impl.WaterRipple;
 15 import com.google.code.kaptcha.text.TextProducer;
 16 import com.google.code.kaptcha.text.WordRenderer;
 17 import com.google.code.kaptcha.text.impl.DefaultTextCreator;
 18 import com.google.code.kaptcha.text.impl.DefaultWordRenderer;
 19 import com.google.code.kaptcha.util.ConfigHelper;
 20 
 21 public class Config
 22 {
 23   private Properties properties;
 24   private ConfigHelper helper;
 25   
 26   public Config(Properties properties)
 27   {
 28     this.properties = properties;
 29     this.helper = new ConfigHelper();
 30   }
 31   
 32   /**
 33    * 设置图片是否有边框
 34    * @return
 35    */
 36   public boolean isBorderDrawn()
 37   {
 38     String paramName = "kaptcha.border";
 39     String paramValue = this.properties.getProperty(paramName);
 40     return this.helper.getBoolean(paramName, paramValue, true);
 41   }
 42   
 43   /**
 44    * 边框颜色   合法值: r,g,b (and optional alpha) 或者 white,black,blue.
 45    * @return
 46    */
 47   public Color getBorderColor()
 48   {
 49     String paramName = "kaptcha.border.color";
 50     String paramValue = this.properties.getProperty(paramName);
 51     return this.helper.getColor(paramName, paramValue, Color.BLACK);
 52   }
 53   
 54   /**
 55    * 边框厚度  合法值:>0
 56    * @return
 57    */
 58   public int getBorderThickness()
 59   {
 60     String paramName = "kaptcha.border.thickness";
 61     String paramValue = this.properties.getProperty(paramName);
 62     return this.helper.getPositiveInt(paramName, paramValue, 1);
 63   }
 64   
 65   /**
 66    * 文本集合,验证码值从此集合中获取
 67    * @return
 68    */
 69   public char[] getTextProducerCharString()
 70   {
 71     String paramName = "kaptcha.textproducer.char.string";
 72     String paramValue = this.properties.getProperty(paramName);
 73     return this.helper.getChars(paramName, paramValue, "abcde2345678gfynmnpwx".toCharArray());
 74   }
 75   
 76   /**
 77    * 验证码长度
 78    * @return
 79    */
 80   public int getTextProducerCharLength()
 81   {
 82     String paramName = "kaptcha.textproducer.char.length";
 83     String paramValue = this.properties.getProperty(paramName);
 84     return this.helper.getPositiveInt(paramName, paramValue, 5);
 85   }
 86   
 87   /**
 88    * 字体类型
 89    * @param fontSize 见Font中的定义
 90    * @return
 91    */
 92   public Font[] getTextProducerFonts(int fontSize)
 93   {
 94     String paramName = "kaptcha.textproducer.font.names";
 95     String paramValue = this.properties.getProperty(paramName);
 96     return this.helper.getFonts(paramName, paramValue, fontSize, new Font[] { new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize) });
 97   }
 98   
 99   /**
100    * 字体大小
101    * @return
102    */
103   public int getTextProducerFontSize()
104   {
105     String paramName = "kaptcha.textproducer.font.size";
106     String paramValue = this.properties.getProperty(paramName);
107     return this.helper.getPositiveInt(paramName, paramValue, 40);
108   }
109   
110   /**
111    * 字体颜色  rgb颜色或者Color中的值
112    * @return
113    */
114   public Color getTextProducerFontColor()
115   {
116     String paramName = "kaptcha.textproducer.font.color";
117     String paramValue = this.properties.getProperty(paramName);
118     return this.helper.getColor(paramName, paramValue, Color.BLACK);
119   }
120   
121   /**
122    * 干扰线的颜色
123    * @return
124    */
125   public Color getNoiseColor()
126   {
127     String paramName = "kaptcha.noise.color";
128     String paramValue = this.properties.getProperty(paramName);
129     return this.helper.getColor(paramName, paramValue, Color.BLACK);
130   }
131     
132   /**
133    * 背景颜色渐变色开始色  rgb或者Color中定义的
134    * @return
135    */
136   public Color getBackgroundColorFrom()
137   {
138     String paramName = "kaptcha.background.clear.from";
139     String paramValue = this.properties.getProperty(paramName);
140     return this.helper.getColor(paramName, paramValue, Color.LIGHT_GRAY);
141   }
142   
143   /**
144    * 背景颜色渐变色结束色   rgb或者Color中定义的
145    * @return
146    */
147   public Color getBackgroundColorTo()
148   {
149     String paramName = "kaptcha.background.clear.to";
150     String paramValue = this.properties.getProperty(paramName);
151     return this.helper.getColor(paramName, paramValue, Color.WHITE);
152   }
153   
154   /**
155    * 图片的宽度
156    * @return
157    */
158   public int getWidth()
159   {
160     String paramName = "kaptcha.image.width";
161     String paramValue = this.properties.getProperty(paramName);
162     return this.helper.getPositiveInt(paramName, paramValue, 200);
163   }
164   
165   /**
166    * 图片的高度
167    * @return
168    */
169   public int getHeight()
170   {
171     String paramName = "kaptcha.image.height";
172     String paramValue = this.properties.getProperty(paramName);
173     return this.helper.getPositiveInt(paramName, paramValue, 50);
174   }
175   
176   /**
177    * 图片的session key
178    * @return
179    */
180   public String getSessionKey()
181   {
182     return this.properties.getProperty("kaptcha.session.key", "KAPTCHA_SESSION_KEY");
183   }
184   
185   public Properties getProperties()
186   {
187     return this.properties;
188   }
189   
190   /**
191    * 生成默认的图片生产者实现
192    * @return
193    */
194   public Producer getProducerImpl()
195   {
196     String paramName = "kaptcha.producer.impl";
197     String paramValue = this.properties.getProperty(paramName);
198     Producer producer = (Producer)this.helper.getClassInstance(paramName, paramValue, new DefaultKaptcha(), this);
199     return producer;
200   }
201   
202   /**
203    * 生成默认的验证码文字生产者实现
204    * @return
205    */
206   public TextProducer getTextProducerImpl()
207   {
208     String paramName = "kaptcha.textproducer.impl";
209     String paramValue = this.properties.getProperty(paramName);
210     TextProducer textProducer = (TextProducer)this.helper.getClassInstance(paramName, paramValue, new DefaultTextCreator(), this);
211     
212     return textProducer;
213   }
214   
215   /**
216    * 文字干扰实现类,默认DefaultNoise,还可以选择com.google.code.kaptcha.impl.NoNoise没有干扰线的实现类
217    * @return
218    */
219   public NoiseProducer getNoiseImpl()
220   {
221       String paramName = "kaptcha.noise.impl";
222       String paramValue = this.properties.getProperty(paramName);
223       NoiseProducer noiseProducer = (NoiseProducer)this.helper.getClassInstance(paramName, paramValue, new DefaultNoise(), this);
224       
225       return noiseProducer;
226   }
227   
228   /**
229    * 图片样式的实现类,默认WaterRipple(水纹),还有下面2种可选
230    * 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy    阴影com.google.code.kaptcha.impl.ShadowGimpy
231    * 
232    * @return
233    */
234   public GimpyEngine getObscurificatorImpl()
235   {
236     String paramName = "kaptcha.obscurificator.impl";
237     String paramValue = this.properties.getProperty(paramName);
238     GimpyEngine gimpyEngine = (GimpyEngine)this.helper.getClassInstance(paramName, paramValue, new WaterRipple(), this);
239     return gimpyEngine;
240   }
241   
242   /**
243    * 文字渲染实现类,默认DefaultWordRenderer,也只有这一个默认的实现类
244    * @return
245    */
246   public WordRenderer getWordRendererImpl()
247   {
248     String paramName = "kaptcha.word.impl";
249     String paramValue = this.properties.getProperty(paramName);
250     WordRenderer wordRenderer = (WordRenderer)this.helper.getClassInstance(paramName, paramValue, new DefaultWordRenderer(), this);
251     
252     return wordRenderer;
253   }
254   
255   /**
256    * 背景图片实现类,默认DefaultBackground,也只有这一个默认实现类
257    * @return
258    */
259   public BackgroundProducer getBackgroundImpl()
260   {
261     String paramName = "kaptcha.background.impl";
262     String paramValue = this.properties.getProperty(paramName);
263     BackgroundProducer backgroundProducer = (BackgroundProducer)this.helper.getClassInstance(paramName, paramValue, new DefaultBackground(), this);
264     
265     return backgroundProducer;
266   }
267 }

看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》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux安装sambaJenkins 九: 小技巧下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

详解Intellij IDEA中.properties文件中文显示乱码问题的解决

首先,你可能会见到如下提示: File encoding is disabled because .properties file (see Settings|Editor|File Encodings|Properties Files) 具体如下图。没截全图,太大了,只截取了提示部分。 在这之前,我一直以为,我已经设置了我这个编辑器下的所有文件的编码格...

解决 java命令行运行class文件时报“错误:找不到或无法加载主类”

问题描述: 今天准备开始复习一下jvm参数,在 perfma 社区里正好有这么一个小课程:https://club.perfma.com/course 从第一节开始复习时,大佬在课后留了一个问题,所以最好自己在java命令行中运行验证一下。结果没想到就碰到了“错误:找不到或无法加载主类”这个问题。 程序都没运行起来,怎么验证jvm参数啊??? 于是前后耗时...

SpringBoot---基本了解

什么是 Spring Boot?   Spring Boot 是 Spring 开源组织下的子项目,是 Spring 组件一站式解决方案,主要是简化了使用 Spring 的难度,简省了繁重的配置,提供了各种启动器,开发者能快速上手。Spring Boot 的最大的优势是“约定优于配置“ SpringBoot约定优于配置的体现   一、Maven的目录结构...

hbase协处理器编码实例

Observer协处理器通常在一个特定的事件(诸如Get或Put)之前或之后发生,相当于RDBMS中的触发器。Endpoint协处理器则类似于RDBMS中的存储过程,因为它可以让你在RegionServer上对数据执行自定义计算,而不是在客户端上执行计算。 本文是以上两者的简单实例,使用的环境:环境 jdk1.8 hadoop2.6.5 hbase1.2....

[JAR包] android引入JAR包,打包成JAR包,打包成Library项目,导入Library项目

(1)项目导入JAR包:1、在项目目录里建立一个libs目录,将外部jar包拷贝在里面。2、右键点击项目,Bulid Path->Configure Build Path3、在设置Libraies项,选择刚才的位置添加jar包。3、在Order and Export项里,将外部jar包选中。4、clean项目后,重新编译,这时的apk包里应该是包含外...

网络干货,无论是运维还是开发都要知道的网络知识系列之(七)

DNS介绍 DNS是什么?DNS(Domain Name System) 称为域名系统,在网站运行中器到了至关重要的作用,主要作用是负责把网站域名解析为对应的IP地址。 例如将www.etiantian.org解析为对应的IP地址记录如1.1.1.1,这个从域名到IP的解析过程,被称为A记录 设置CNAME别名记录,这个别名解析功能常被CDN加速服务...