<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg>
<props>
<prop key="kaptcha.border">yes</prop>
<prop key="kaptcha.border.color">105,179,90</prop>
<prop key="kaptcha.textproducer.font.color">blue</prop>
<prop key="kaptcha.image.width">125</prop>
<prop key="kaptcha.image.height">45</prop>
<prop key="kaptcha.textproducer.font.size">45</prop>
<prop key="kaptcha.session.key">code</prop>
<prop key="kaptcha.textproducer.char.length">4</prop>
<prop key="kaptcha.textproducer.font.names">宋體,楷體,微軟雅黑</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
package com.kjs.web.controller.common;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
/**
* Created by 李恒名 on 2016/6/23.
*/
@Controller
public class CaptchaController {
@Autowired
private Producer captchaProducer;
@RequestMapping("/captcha-image")
public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String capText = captchaProducer.createText();
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
System.out.println("驗(yàn)證碼: " + capText );
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
return null;
}
}```
#####可選配置項(xiàng)
kaptcha.border 是否有邊框 默認(rèn)為true 我們可以自己設(shè)置yes,no
kaptcha.border.color 邊框顏色 默認(rèn)為Color.BLACK
kaptcha.border.thickness 邊框粗細(xì)度 默認(rèn)為1
kaptcha.producer.impl 驗(yàn)證碼生成器 默認(rèn)為DefaultKaptcha
kaptcha.textproducer.impl 驗(yàn)證碼文本生成器 默認(rèn)為DefaultTextCreator
kaptcha.textproducer.char.string 驗(yàn)證碼文本字符內(nèi)容范圍 默認(rèn)為abcde2345678gfynmnpwx
kaptcha.textproducer.char.length 驗(yàn)證碼文本字符長(zhǎng)度 默認(rèn)為5
kaptcha.textproducer.font.names 驗(yàn)證碼文本字體樣式 默認(rèn)為new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
kaptcha.textproducer.font.size 驗(yàn)證碼文本字符大小 默認(rèn)為40
kaptcha.textproducer.font.color 驗(yàn)證碼文本字符顏色 默認(rèn)為Color.BLACK
kaptcha.textproducer.char.space 驗(yàn)證碼文本字符間距 默認(rèn)為2
kaptcha.noise.impl 驗(yàn)證碼噪點(diǎn)生成對(duì)象 默認(rèn)為DefaultNoise
kaptcha.noise.color 驗(yàn)證碼噪點(diǎn)顏色 默認(rèn)為Color.BLACK
kaptcha.obscurificator.impl 驗(yàn)證碼樣式引擎 默認(rèn)為WaterRipple
kaptcha.word.impl 驗(yàn)證碼文本字符渲染 默認(rèn)為DefaultWordRenderer
kaptcha.background.impl 驗(yàn)證碼背景生成器 默認(rèn)為DefaultBackground
kaptcha.background.clear.from 驗(yàn)證碼背景顏色漸進(jìn) 默認(rèn)為Color.LIGHT_GRAY
kaptcha.background.clear.to 驗(yàn)證碼背景顏色漸進(jìn) 默認(rèn)為Color.WHITE
kaptcha.image.width 驗(yàn)證碼圖片寬度 默認(rèn)為200
kaptcha.image.height 驗(yàn)證碼圖片高度 默認(rèn)為50