亲宝软件园·资讯

展开

kaptcha验证码使用方法 kaptcha验证码使用方法详解

实践不断改变思想 人气:0

本文为大家分享了kaptcha验证码的使用方法,供大家参考,具体内容如下

1.首先在pom.xml文件中导入Maven依赖

<dependency>
  <groupId>com.github.penggle</groupId>
  <artifactId>kaptcha</artifactId>
  <version>2.3.2</version>
</dependency>

2.在web.xml文件中配置一个servlet

<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.页面代码如下

<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>

向后端提交数据

var verifyCodeActual = $('#j_captcha').val();
 if (!verifyCodeActual) {
  $.toast('请输入验证码!');
  return;
 }
 formData.append("verifyCodeActual", verifyCodeActual);

4.js交互

function changeVerifyCode(img) {
 img.src = "../Kaptcha?" + Math.floor(Math.random() * 100);
}

5.后端验证

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;
 }
}
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;
 }

 }
}

加载全部内容

相关教程
猜你喜欢
用户评论