Java如何实现kaptcha网页验证码验证
作者:一个爱运动的程序员
Java实现kaptcha网页验证码验证
首先来了解一下什么是验证码:
- 验证码(CAPTCHA) ∶是一种区分用户是计算机还是人的公共全自动程序。
- 作用∶可以防止恶意破解密码、刷票、论坛灌水,有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试。实际上用验证码是现在很多网站通行的方式(比如招商银行的网上个人银行,百度社区),我们利用比较简易的方式实现了这个功能。
下面便一起来了解kaptcha吧
首先要了解什么是kaptcha?
- 它一个java开源的验证码工具包,kaptcha 是一个非常实用的验证码生成工具,可以通过配置生成多样化的验证码。
- 当然除了kaptcha可以实现验证码外,还可以通过servlet、jcaptcha来实现验证码。
kaptcha工作的原理:
- 调用 com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。
- 同时将生成的验证码字符串放到 HttpSession中。
相关文章:
SpringBoot整合kaptcha验证码,复制粘贴即可用
我的项目目录:
一、引入Maven依赖
pom.xml:
<!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
二、编写Servlet
kaptcha可配置项参考:
kaptcha.border --------------------------------------------------是否有边框 默认为true 我们可以自己设置yes,no
kaptcha.border.color -------------------------------------------边框颜色 默认为Color.BLACK
kaptcha.border.thickness -------------------------------------边框粗细度 默认为1
kaptcha.producer.impl -----------------------------------------验证码生成器 默认为DefaultKaptcha
kaptcha.textproducer.impl ------------------------------------验证码文本生成器 默认为DefaultTextCreator
kaptcha.textproducer.char.string ----------------------------验证码文本字符内容范围 默认为abcde2345678gfynmnpwx
kaptcha.textproducer.char.length ---------------------------验证码文本字符长度 默认为5
kaptcha.textproducer.font.names ---------------------------验证码文本字体样式 默认为new Font(“Arial”, 1, fontSize), new Font(“Courier”, 1, fontSize)
kaptcha.textproducer.font.size ------------------------------验证码文本字符大小 默认为40
kaptcha.textproducer.font.color -----------------------------验证码文本字符颜色 默认为Color.BLACK
kaptcha.textproducer.char.space -------------------------- 验证码文本字符间距 默认为2
kaptcha.noise.impl ----------------------------------------------验证码噪点生成对象 默认为DefaultNoise
kaptcha.noise.color ---------------------------------------------验证码噪点颜色 默认为Color.BLACK
kaptcha.obscurificator.impl ------------------------------------验证码样式引擎 默认为WaterRipple
kaptcha.word.impl -----------------------------------------------验证码文本字符渲染 默认为DefaultWordRenderer
kaptcha.background.impl --------------------------------------验证码背景生成器 默认为DefaultBackground
kaptcha.background.clear.from ------------------------------验证码背景颜色渐进 默认为Color.LIGHT_GRAY
kaptcha.background.clear.to ----------------------------------验证码背景颜色渐进 默认为Color.WHITE
kaptcha.image.width --------------------------------------------验证码图片宽度 默认为200
kaptcha.image.height -------------------------------------------验证码图片高度 默认为50
在web.xml文件中配置kaptcha与它的servlet映射等。
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- 登陆验证码Kaptcha 2--> <servlet> <servlet-name>Kaptcha</servlet-name> <servlet-class> com.google.code.kaptcha.servlet.KaptchaServlet </servlet-class> <init-param> <description>图片边框,合法值:yes , no</description> <param-name>kaptcha.border</param-name> <param-value>yes</param-value> </init-param> <init-param> <description> 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. </description> <param-name>kaptcha.border.color</param-name> <param-value>black</param-value> </init-param> <init-param> <description>边框厚度,合法值:>0</description> <param-name>kaptcha.border.thickness</param-name> <param-value>1</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>图片实现类</description> <param-name>kaptcha.producer.impl</param-name> <param-value> com.google.code.kaptcha.impl.DefaultKaptcha </param-value> </init-param> <init-param> <description>文本实现类</description> <param-name>kaptcha.textproducer.impl</param-name> <param-value> com.google.code.kaptcha.text.impl.DefaultTextCreator </param-value> </init-param> <init-param> <description>文本集合,验证码值从此集合中获取</description> <param-name>kaptcha.textproducer.char.string</param-name> <param-value>1234567890</param-value> <!--<param-value>abcde2345678gfynmnpwx</param-value>--> </init-param> <init-param> <description>验证码长度 5</description> <param-name>kaptcha.textproducer.char.length</param-name> <param-value>5</param-value> </init-param> <init-param> <description>字体 Arial, Courier</description> <param-name>kaptcha.textproducer.font.names</param-name> <param-value>Arial, Courier</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. </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>干扰实现类</description> <param-name>kaptcha.noise.impl</param-name> <param-value> <!-- com.google.code.kaptcha.impl.NoNoise --> com.google.code.kaptcha.impl.DefaultNoise </param-value> </init-param> <init-param> <description> 干扰颜色,合法值: r,g,b 或者 white,black,blue. </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>背景实现类</description> <param-name>kaptcha.background.impl</param-name> <param-value> com.google.code.kaptcha.impl.DefaultBackground </param-value> </init-param> <init-param> <description>背景颜色渐变,开始颜色</description> <param-name>kaptcha.background.clear.from</param-name> <param-value> black</param-value> </init-param> <init-param> <description>背景颜色渐变,结束颜色</description> <param-name>kaptcha.background.clear.to</param-name> <param-value>white</param-value> </init-param> <init-param> <description>文字渲染器</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键 </description> <param-name>kaptcha.session.key</param-name> <param-value>KAPTCHA_SESSION_KEY</param-value> </init-param> <init-param> <description> 生成kaptcha的日期被放入HttpSession中。这是会话中该项的键值。 </description> <param-name>kaptcha.session.date</param-name> <param-value>KAPTCHA_SESSION_DATE</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Kaptcha</servlet-name> <url-pattern>/randomcode.jpg</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
三、编写:页面、判断与跳转
1、只有数字的验证码
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>randomcode</title> <script type="text/javascript"> function changeR(node){ // 用于点击时产生不同的验证码 node.src = "randomcode.jpg?time="+new Date().getTime() ; } </script> </head> <body> <img alt="random" src="randomcode.jpg" onclick="changeR(this)" style="cursor: pointer;"> <form action="check.jsp"> <input type="text" name="r"> <input type="submit" value="确认"> </form> </body> </html>
check.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>check</title> </head> <body> <% // 检查是否是正确的验证码 String k = (String) session .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); String str = request.getParameter("r"); str = new String(str.getBytes("iso-8859-1"),"utf-8"); if (k.equals(str)) out.print("输入验证码正确!"); else out.print("输入验证码错误!!!"); out.print("验证码为:" + k + ",你输入的验证码为:" + str); %> </body> </html>
结果为:
2、含义数字、字母、中文的验证码
以上便是数字验证码,那如果加字母和中文 呢???
更改web.xml的文本实现类,导入自己创建的类:
我的web.xml的引入和我类的位置:(自行寻找到进行更换)
<init-param> <description>文本实现类</description> <param-name>kaptcha.textproducer.impl</param-name> <param-value> com.kaptcha.ChineseText </param-value> </init-param>
ChineseText.java:
package com.kaptcha; import java.util.Random; import com.google.code.kaptcha.text.TextProducer; import com.google.code.kaptcha.util.Configurable; public class ChineseText extends Configurable implements TextProducer { public String getText() { int length = getConfig().getTextProducerCharLength(); String finalWord = "", firstWord = ""; int tempInt = 0; String[] array = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f","g", "h", "i", "j", "k"}; Random rand = new Random(); for (int i = 0; i < length; i++) { switch (rand.nextInt(array.length)) { case 1: tempInt = rand.nextInt(26) + 65; firstWord = String.valueOf((char) tempInt); break; case 2: int r1, r2, r3, r4; String strH, strL; // high&low r1 = rand.nextInt(3) + 11; if (r1 == 13) { r2 = rand.nextInt(7); } else { r2 = rand.nextInt(16); } r3 = rand.nextInt(6) + 10; if (r3 == 10) { r4 = rand.nextInt(15) + 1; } else if (r3 == 15) { r4 = rand.nextInt(15); } else { r4 = rand.nextInt(16); } strH = array[r1] + array[r2]; strL = array[r3] + array[r4]; byte[] bytes = new byte[2]; bytes[0] = (byte) (Integer.parseInt(strH, 16)); bytes[1] = (byte) (Integer.parseInt(strL, 16)); firstWord = new String(bytes); break; default: tempInt = rand.nextInt(10) + 48; firstWord = String.valueOf((char) tempInt); break; } finalWord += firstWord; } return finalWord; } }
这个的效果:
3、两个数相加的验证码
运行都是OK的,学到这里那两个数的算式验证码应该如何实现呢???
首先,我们就要更改servlet里的kaptcha的默认的KaptchaServlet,自己在写一个,还有就是我们的是两个数的算法,那也就要更改验证码长度的长度,还有更改我们上面实现的中文和字母换回默认值。
先更改一下web.xml:(自行寻找进行更换)
<servlet-class> com.kaptcha.KaptchaServlet </servlet-class>
<init-param> <description>文本实现类</description> <param-name>kaptcha.textproducer.impl</param-name> <param-value> com.google.code.kaptcha.text.impl.DefaultTextCreator </param-value> </init-param>
<init-param> <description>验证码长度 2</description> <param-name>kaptcha.textproducer.char.length</param-name> <param-value>2</param-value> </init-param>
KaptchaServlet.java:
package com.kaptcha; import com.google.code.kaptcha.Producer; import com.google.code.kaptcha.util.Config; import javax.imageio.ImageIO; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; public class KaptchaServlet extends HttpServlet implements Servlet { private Properties props; private Producer kaptchaProducer; private String sessionKeyValue; public KaptchaServlet() { this.props = new Properties(); this.kaptchaProducer = null; this.sessionKeyValue = null; } public void init(ServletConfig conf) throws ServletException { super.init(conf); ImageIO.setUseCache(false); Enumeration initParams = conf.getInitParameterNames(); while (initParams.hasMoreElements()) { String key = (String) initParams.nextElement(); String value = conf.getInitParameter(key); this.props.put(key, value); } Config config = new Config(this.props); this.kaptchaProducer = config.getProducerImpl(); this.sessionKeyValue = config.getSessionKey(); } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setDateHeader("Expires", 0L); resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); resp.addHeader("Cache-Control", "post-check=0, pre-check=0"); resp.setHeader("Pragma", "no-cache"); resp.setContentType("image/jpeg"); String capText = this.kaptchaProducer.createText(); String s1 = capText.substring(0, 1); String s2 = capText.substring(1, 2); int r = Integer.valueOf(s1).intValue() + Integer.valueOf(s2).intValue(); req.getSession().setAttribute(this.sessionKeyValue, String.valueOf(r)); BufferedImage bi = this.kaptchaProducer.createImage(s1+"+"+s2+"=?"); ServletOutputStream out = resp.getOutputStream(); ImageIO.write(bi, "jpg", out); try { out.flush(); } finally { out.close(); } } }
效果为:
总结
以上便是比较简单的java实现验证码的操作
这些仅为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。