本文为大家分享了kaptcha验证码的使用方法,供大家参考,具体内容如下
1.首先在pom.xml文件中导入maven依赖
1
2
3
4
5
|
<dependency>
<groupid>com.github.penggle</groupid>
<artifactid>kaptcha</artifactid>
<version> 2.3 . 2 </version>
</dependency>
|
2.在web.xml文件中配置一个servlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
<servlet>
<!-- 生成图片的servlet -->
<servlet-name>kaptcha</servlet-name>
<servlet- class >com.google.code.kaptcha.servlet.kaptchaservlet</servlet- class >
<!-- 是否有边框 -->
<init-param>
<param-name>kaptcha.border</param-name>
<param-value>no</param-value>
</init-param>
<!-- 字体颜色 -->
<init-param>
<param-name>kaptcha.textproducer.font.color</param-name>
<param-value>red</param-value>
</init-param>
<!-- 图片宽度 -->
<init-param>
<param-name>kaptcha.image.width</param-name>
<param-value> 135 </param-value>
</init-param>
<!-- 使用哪些字符生成验证码 -->
<init-param>
<param-name>kaptcha.textproducer. char .string</param-name>
<param-value>acdefhkprstwx345679</param-value>
</init-param>
<!-- 图片高度 -->
<init-param>
<param-name>kaptcha.image.height</param-name>
<param-value> 50 </param-value>
</init-param>
<!-- 字体大小 -->
<init-param>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value> 43 </param-value>
</init-param>
<!-- 干扰线的颜色 -->
<init-param>
<param-name>kaptcha.noise.color</param-name>
<param-value>black</param-value>
</init-param>
<!-- 字符个数 -->
<init-param>
<param-name>kaptcha.textproducer. char .length</param-name>
<param-value> 4 </param-value>
</init-param>
<!-- 使用哪些字体 -->
<init-param>
<param-name>kaptcha.textproducer.font.names</param-name>
<param-value>arial</param-value>
</init-param>
</servlet>
<!-- 映射的url -->
<servlet-mapping>
<servlet-name>kaptcha</servlet-name>
<url-pattern>/kaptcha</url-pattern>
</servlet-mapping>
|
3.页面代码如下
1
2
3
4
5
6
7
8
9
|
<div class = "item-inner" >
<label for = "j_captcha" class = "item-title label" >验证码</label>
<input id= "j_captcha" name= "j_captcha" type= "text"
class = "form-control in" placeholder= "验证码" />
<div class = "item-input" >
<img id= "captcha_img" alt= "点击更换" title= "点击更换"
onclick= "changeverifycode(this)" src= "../kaptcha" />
</div>
</div>
|
向后端提交数据
1
2
3
4
5
6
|
var verifycodeactual = $( '#j_captcha' ).val();
if (!verifycodeactual) {
$.toast( '请输入验证码!' );
return ;
}
formdata.append( "verifycodeactual" , verifycodeactual);
|
4.js交互
1
2
3
|
function changeverifycode(img) {
img.src = "../kaptcha?" + math.floor(math.random() * 100 );
}
|
5.后端验证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import javax.servlet.http.httpservletrequest;
public class codeutil {
public static boolean checkverifycode(httpservletrequest request) {
string verifycodeexpected = (string) request.getsession().getattribute(
com.google.code.kaptcha.constants.kaptcha_session_key);
string verifycodeactual = httpservletrequestutil.getstring(request,
"verifycodeactual" );
if (verifycodeactual == null
|| !verifycodeactual.equalsignorecase(verifycodeexpected)) {
return false ;
}
return true ;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import javax.servlet.http.httpservletrequest;
public class httpservletrequestutil {
public static int getint(httpservletrequest request, string name) {
try {
return integer.decode(request.getparameter(name));
} catch (exception e) {
return - 1 ;
}
}
public static long getlong(httpservletrequest request, string name) {
try {
return long .valueof(request.getparameter(name));
} catch (exception e) {
return - 1 ;
}
}
public static double getdouble(httpservletrequest request, string name) {
try {
return double .valueof(request.getparameter(name));
} catch (exception e) {
return -1d;
}
}
public static boolean getboolean(httpservletrequest request, string name) {
try {
return boolean .valueof(request.getparameter(name));
} catch (exception e) {
return false ;
}
}
public static string getstring(httpservletrequest request, string name) {
try {
string result = request.getparameter(name);
if (result != null ) {
result = result.trim();
}
if ( "" .equals(result))
result = null ;
return result;
} catch (exception e) {
return null ;
}
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/xinzai245/article/details/84726987