9.kaptcha

  • pom.xml:
<dependency>
    <groupId>com.github.penggle</groupId>
    <artifactId>kaptcha</artifactId>
    <version>2.3.2</version>
</dependency>
  • web.xml:
<servlet>
    <servlet-name>myCaptcha</servlet-name>
    <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
    <!-- <init-param> ... </init-param> -->
</servlet>
<servlet-mapping>
    <servlet-name>myCaptcha</servlet-name>
    <!-- 前台获取验证码图片的src地址 -->
    <url-pattern>/kaptcha</url-pattern>
</servlet-mapping>
  • html:
<img id="captcha_img" alt="验证码" title="点击更换" onclick="changeVerifyCode(this)" src="../kaptcha">
  • js:
// 点击更换验证码实现
function changeVerifyCode(img) {
	img.src='../kaptcha?' + Math.floor(Math.random() * 100);
}
  • Jquery
// 用户提交表单时,将验证码一起传输过去
var verifyCodeActrual =  $('#j_captcha').val();

// 其他的一些认证
// *****
if(!verifyCodeActrual) {
	$.toast('请输入验证码');
	return;
}
formData.append('verifyCodeActrual', verifyCodeActrual);
  • 怎么验证用户写的验证码的正确性?

1 获取用户传过来的字符串:

String verifyCodeActural = request.getParameter("verifyCodeActural");
if (StringUtils.isBlank(verifyCodeActural) {
    return ***;
}

2 获取Kaptcha生成的正确字符串:

// import:com.google.code.kaptcha.Constants
String verifyCodeExpected = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
if (StringUtils.isBlank(verifyCodeExpected)) {
	return false;
}

3 比较两者是否相等:

return verifyCodeExpected.equalsIgnoreCase(verifyCodeActural)) ;
  • 配置详解:
<init-param>
    <description>图片边框,合法值yes,no,默认值yes</description>
    <param-name>kaptcha.border</param-name>
    <param-value>no</param-value>
</init-param>
<init-param>
    <description>边框颜色,合法值rgb(and optional alpha)或者 white,black,blue,默认值black</description>
    <param-name>kaptcha.border.color</param-name>
    <param-value>blue</param-value>
</init-param>
<init-param>
    <description>边框厚度,合法值>0,默认值为1</description>
    <param-name>kaptcha.border.thickness</param-name>
    <param-value>2</param-value>
</init-param>
<init-param>
    <description>图片宽度,默认值200</description>
    <param-name>kaptcha.image.width</param-name>
    <param-value>200</param-value>
</init-param>
<init-param>
    <description>图片高度,默认值50</description>
    <param-name>kaptcha.image.height</param-name>
    <param-value>50</param-value>
</init-param>
<init-param>
    <description>图片实现类,默认值com.google.code.kaptcha.impl.DefaultKaptcha</description>
    <param-name>kaptcha.producer.impl</param-name>
    <param-value>com.google.code.kaptcha.impl.DefaultKaptcha</param-value>
</init-param>
<init-param>
    <description>文本实现类,默认值com.google.code.kaptcha.text.impl.DefaultTextCreator</description>
    <param-name>kaptcha.textproducer.impl</param-name>
    <param-value>com.google.code.kaptcha.text.impl.DefaultTextCreator</param-value>
</init-param>
<init-param>
    <description>文本集合,验证码值从此集合中获取,默认值abcde2345678gfynmnpwx</description>
    <param-name>kaptcha.textproducer.char.string</param-name>
    <param-value>abcde2345678gfynmnpwx</param-value>
</init-param>
<init-param>
    <description>验证码长度,默认值为5</description>
    <param-name>kaptcha.textproducer.char.length</param-name>
    <param-value>4</param-value>
</init-param>
<init-param>
    <description>字体,默认值Arial, Courier(如果使用中文验证码,则必须使用中文的字体,否则出现乱码)</description>
    <param-name>kaptcha.textproducer.font.names</param-name>
    <param-value>微软雅黑</param-value>
</init-param>
<init-param>
    <description>字体大小,默认值为40px</description>
    <param-name>kaptcha.textproducer.font.size</param-name>
    <param-value>40</param-value>
</init-param>
<init-param>
    <description>字体颜色,合法值: r,g,b 或者 white,black,blue,默认值black</description>
    <param-name>kaptcha.textproducer.font.color</param-name>
    <param-value>black</param-value>
</init-param>
<init-param>
    <description>文字间隔,默认值为2</description>
    <param-name>kaptcha.textproducer.char.space</param-name>
    <param-value>2</param-value>
</init-param>
<init-param>
    <description>干扰实现类,默认值com.google.code.kaptcha.impl.DefaultNoise</description>
    <param-name>kaptcha.noise.impl</param-name>
    <param-value>com.google.code.kaptcha.impl.DefaultNoise</param-value>
</init-param>
<init-param>
    <description>干扰 颜色,合法值: r,g,b 或者 white,black,blue,默认值black</description>
    <param-name>kaptcha.noise.color</param-name>
    <param-value>black</param-value>
</init-param>
<init-param>
    <description>图片样式: 
    水纹com.google.code.kaptcha.impl.WaterRipple 
    鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
    阴影com.google.code.kaptcha.impl.ShadowGimpy,默认值水纹</description>
    <param-name>kaptcha.obscurificator.impl</param-name>
    <param-value>com.google.code.kaptcha.impl.WaterRipple</param-value>
</init-param>
<init-param>
    <description>背景实现类,默认值com.google.code.kaptcha.impl.DefaultBackground</description>
    <param-name>kaptcha.background.impl</param-name>
    <param-value>com.google.code.kaptcha.impl.DefaultBackground</param-value>
</init-param>
<init-param>
    <description>背景颜色渐变,开始颜色,默认值lightGray</description>
    <param-name>kaptcha.background.clear.from</param-name>
    <param-value>lightGray</param-value>
</init-param>
<init-param>
    <description>背景颜色渐变, 结束颜色,默认值white</description>
    <param-name>kaptcha.background.clear.to</param-name>
    <param-value>white</param-value>
</init-param>
<init-param>
    <description>文字渲染器,默认值com.google.code.kaptcha.text.impl.DefaultWordRenderer</description>
    <param-name>kaptcha.word.impl</param-name>
    <param-value>com.google.code.kaptcha.text.impl.DefaultWordRenderer</param-value>
</init-param>
<init-param>
    <description>session key,默认值KAPTCHA_SESSION_KEY</description>
    <param-name>kaptcha.session.key</param-name>
    <param-value>KAPTCHA_SESSION_KEY</param-value>
</init-param>
<init-param>
    <description>session date,默认值KAPTCHA_SESSION_DATE</description>
    <param-name>kaptcha.session.date</param-name>
    <param-value>KAPTCHA_SESSION_DATE</param-value>
</init-param>
posted @ 2018-11-16 00:04  stdio_0  阅读(186)  评论(0编辑  收藏  举报