javaweb中验证码插件Kaptcha的使用
1.
首先在pom.xml中导入依赖
<!-- 生成验证码 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2.
在web.xml中设置
<!-- 后台生成验证码 -->
<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>
<servlet-mapping>
<servlet-name>Kaptcha</servlet-name>
<url-pattern>/Kaptcha</url-pattern>
</servlet-mapping>
3.
前端页面
<form id="mainForm" role="form" action="${pageContext.request.contextPath }/user/subLogin" method="post">
<div class="form-group">
<input type="text" class="form-control" placeholder="验证码" id="j_captcha" name="verifyCodeActual" style="width:60%;">
<div style="float: right;width: 116px;height:46px; margin-top: -44px;">
<img id="captcha_img" alt="点击更换" title="点击更换"
onclick="changeVerifyCode(this)" src="../Kaptcha"/>
</div>
</div>
<button type="button" id="btnLogin" class="btn btn-info" style="width:100%;margin-top:-6px;">登录</button>
</form>
4.
后台controller层中
@RequestMapping(value="/subLogin",method=RequestMethod.POST)
public String sublogin(HttpServletRequest request,String verifyCodeActual,Model model){
//System.out.println(verifyCodeActual);
//System.out.println(request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY));
if(!CodeUtil.checkVerifyCode(request,verifyCodeActual)){
model.addAttribute("msg","验证码输入有误");
return "/showuserpage/userLogin";
}
return "/showuserpage/userIndex";
}
定义一个验证验证码的util类
public class CodeUtil {
public static boolean checkVerifyCode(HttpServletRequest request,String verifyCodeActual){
String verifyCodeExpected = (String) request.getSession()
.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
//String verifyCodeActual = HttpServletRequestUtil.getString(request, "verifyCodeActual");
if(verifyCodeActual == null || !verifyCodeActual.equals(verifyCodeExpected)){
return false;
}
return true;
}
}