项目中引入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的格式的图片给前端,例如:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(bi, "jpeg", outputStream); String base64Image = "data:image/jpeg;base64," + Base64.encodeBase64String(outputStream.toByteArray());