验证码组件----Kaptcha

本文介绍如何使用Kaptcha工具生成验证码并整合到项目中。详细解释了配置过程及如何在Servlet和JSP页面中使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

验证码:

1.验证码的作用,防止恶意注册。
2.在注册页面需要输入验证码,Servlet在处理注册功能时,需要先检查验证码是否正确,如果正确才允许注册,
    否则直接返回注册页面重新输入。


    使用一个第三方工具,来自动生成验证码。kaptcha-2.3.2
    Kaptcha这个工具中有一个Servlet,com.google.code.kaptcha.servlet.KaptchaServlet,我们需要在项目中手动对该Servlet进行映射,当我们通过浏览器去访问这个Servlet,在该Servlet中,首先他会随机生成一个字符串,然后将字符串放入进session域中,最后返回一个由该字符串转换成的图片。
可以在通过Servlet的初始化参数来对Kaptcha进行一个个性化的设置。

 

首先下载kaptcha-2.3.2.jar

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  3   <display-name>bookstore02</display-name>
  4   <welcome-file-list>
  5     <welcome-file>index.html</welcome-file>
  6     <welcome-file>index.htm</welcome-file>
  7     <welcome-file>index.jsp</welcome-file>
  8     <welcome-file>default.html</welcome-file>
  9     <welcome-file>default.htm</welcome-file>
 10     <welcome-file>default.jsp</welcome-file>
 11   </welcome-file-list>
 12   <servlet>
 13     <description></description>
 14     <display-name>RegisterServlet</display-name>
 15     <servlet-name>RegisterServlet</servlet-name>
 16     <servlet-class>com.neuedu.web.RegisterServlet</servlet-class>
 17   </servlet>
 18   <servlet-mapping>
 19     <servlet-name>RegisterServlet</servlet-name>
 20     <url-pattern>/RegisterServlet</url-pattern>
 21   </servlet-mapping>
 22   <servlet>
 23     <description></description>
 24     <display-name>LoginServlet</display-name>
 25     <servlet-name>LoginServlet</servlet-name>
 26     <servlet-class>com.neuedu.web.LoginServlet</servlet-class>
 27   </servlet>
 28   <servlet-mapping>
 29     <servlet-name>LoginServlet</servlet-name>
 30     <url-pattern>/LoginServlet</url-pattern>
 31   </servlet-mapping>
 32   
    <!--配置验证码组件kaptcha --> 83 <servlet> 84 <servlet-name>KaptchaServlet</servlet-name> 85 <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> 86 87 <init-param> 88 <param-name>kaptcha.textproducer.char.length</param-name> 89 <param-value>4</param-value> 90 </init-param> 91 92 <init-param> 93 <param-name>kaptcha.textproducer.char.space</param-name> 94 <param-value>6</param-value> 95 </init-param> 96 97 <init-param> 98 <param-name>kaptcha.session.key</param-name> 99 <param-value>code</param-value> 100 </init-param> 101 </servlet> 102 103 <servlet-mapping> 104 <servlet-name>KaptchaServlet</servlet-name> 105 <url-pattern>/code.jpg</url-pattern> 106 </servlet-mapping> 107 108 109 </web-app>

验证码在jsp页面的配置信息:

 

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath }/static/css/style.css" >
<script type="text/javascript" src="${pageContext.request.contextPath }/static/script/jquery-1.7.2.js"></script>
<script type="text/javascript">
    $(function(){
        $("#code01").click(function(){
            this.src="${pageContext.request.contextPath}/code.jpg?t="+Math.random();
        }); 
    });
</script>
</head>
<body>
   <div class="form">
    <form action="${pageContext.request.contextPath }/UserServlet?method=regist" method="post">
        <label>用户名称:</label>
        <input class="itxt" type="text" placeholder="请输入用户名" autocomplete="off" tabindex="1" name="username" />
        <br />
        <br />
        <label>用户密码:</label>
        <input class="itxt" type="password" placeholder="请输入密码" autocomplete="off" tabindex="1" name="password" />
        <br />
        <br />
        <label>确认密码:</label>
        <input class="itxt" type="password" placeholder="确认密码" autocomplete="off" tabindex="1" name="repwd" />
        <br />
        <br />
        <label>验证码:</label>
        <input class="itxt" type="text" style="width: 150px;" name="code"/>
        <img id="code01" alt="" src="${pageContext.request.contextPath}/code.jpg" style="float: right; margin-right: 40px; width:86px;height:40px;">                                    
        <br />
        <br />
        <input type="submit" value="注册" id="sub_btn" />
    </form>    
</div> </body> </html>

 

 

 

 

kaptcha其他配置:

kaptcha.border
验证码图片的边框,可以设置yes或者no
默认值 yes

kaptcha.border.color
边框的颜色reg值。合法值 rgb,black,blue,white
默认值 black

kaptcha.border.thickness
边框的宽度
默认 1

kaptcha.image.width
图片的宽度
默认200

kaptcha.image.height
图片的高度
默认50

kaptcha.producer.impl
生成图片使用的类
默认 com.google.code.kaptcha.impl.DefaultKaptcha


kaptcha.textproducer.impl
生成图片中文字的使用的类
默认com.google.code.kaptcha.text.impl.DefaultTextCreator

    
kaptcha.textproducer.char.string
验证码中使用的字符
默认 abcde2345678gfynmnpwx

kaptcha.textproducer.char.length
验证码中字符的数量
默认 5

kaptcha.textproducer.font.names
验证码的字体
默认 Arial, Courier

kaptcha.textproducer.font.size
字体的大小
默认 40

kaptcha.textproducer.font.color
字体颜色 rgb值,颜色单词
默认 black

kaptcha.textproducer.char.space
两个字符之间的间距
默认 2

kaptcha.noise.impl
干扰线生成类
默认 com.google.code.kaptcha.impl.DefaultNoise

kaptcha.noise.color
干扰线颜色
默认 black

kaptcha.obscurificator.impl
The obscurificator implementation.
默认 com.google.code.kaptcha.impl.WaterRipple

kaptcha.background.impl
背景颜色设置类
默认 com.google.code.kaptcha.impl.DefaultBackground


kaptcha.background.clear.from
渐变颜色 左到右
默认 light grey

kaptcha.background.clear.to
渐变颜色 右到左
默认 white

kaptcha.word.impl
词语渲染器
默认 com.google.code.kaptcha.text.impl.DefaultWordRenderer

kaptcha.session.key
在session中存储属性的名字
默认 KAPTCHA_SESSION_KEY

转载于:https://www.cnblogs.com/double-s/p/8117814.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值