欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

kaptcha验证码使用方法详解

程序员文章站 2024-03-04 17:23:53
本文为大家分享了kaptcha验证码的使用方法,供大家参考,具体内容如下 1.首先在pom.xml文件中导入maven依赖 ...

本文为大家分享了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;
 }

 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。