———-pom.xml配置
<!-- kaptcha -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
———-web.xml配置
<servlet>
<servlet-name>Kaptcha</servlet-name>
<servlet-class>
com.google.code.kaptcha.servlet.KaptchaServlet
</servlet-class>
/**属性配置可不写,试用默认即可start**/
<init-param>
<param-name>kaptcha.image.width</param-name>
<param-value>200</param-value>
<description>
Width in pixels of the kaptcha image.
</description>
</init-param>
<init-param>
<param-name>kaptcha.image.height</param-name>
<param-value>50</param-value>
<description>
Height in pixels of the kaptcha image.、
</description>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>4</param-value>
<description>
The number of characters to display.
</description>
</init-param>
<init-param>
<param-name>kaptcha.noise.impl</param-name>
<param-value>
com.google.code.kaptcha.impl.NoNoise
</param-value>
<description>The noise producer.</description>
</init-param>
/**属性配置可不写,试用默认即可end**/
</servlet>
<servlet-mapping>
<servlet-name>Kaptcha</servlet-name>
<url-pattern>/kaptcha.jpg</url-pattern>
</servlet-mapping>
———-html使用注意图片id
<div class="control-form">
<input type="text" name="picNo" id="picNo" tabindex="2" class="ui-input valid" placeholder="请输入图片识别码">
<img id="kaptchaImage" src="../sign/kaptcha.jpg" />
</div>
———-调用方法
@Autowired
public void setCaptchaProducer(Producer captchaProducer) {
this.captchaProducer = captchaProducer;
}
/**
*
* @Description 获取图片验证码
* @author pm
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping("/kaptcha.jpg")
@NoLogin
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// Set to expire far in the past.
response.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control",
"no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
// return a jpeg
response.setContentType("image/jpeg");
// create the text for the image
String capText = captchaProducer.createText();
// store the text in the session
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,
capText);
// create the image with the text
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
// write the data out
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
return null;
}
———-验证图片验证码是否正确
@RequestMapping("/checkPic.do")
@NoLogin
public @ResponseBody JsonResp checkPic(SignVo signVo,
HttpServletRequest request) throws Exception {
JsonResp resp = null;
// 验证图片验证码
String kaptchaExpected = (String) request.getSession().getAttribute(
com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
if (!signVo.getPicNo().equals("")) {
if (!signVo.getPicNo().equals(kaptchaExpected)) {
resp = new JsonResp(JsonResp.STATE_ERR);
return resp;
}
}
resp = new JsonResp(JsonResp.STATE_OK);
return resp;
}