• 验证码组件----Kaptcha


    验证码:

    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=" 150px;" name="code"/>
            <img id="code01" alt="" src="${pageContext.request.contextPath}/code.jpg" style="float: right; margin-right: 40px; 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

  • 相关阅读:
    (转)Spring中的事务操作
    (转)Spring使用AspectJ进行AOP的开发:注解方式
    (转)SpringMVC学习(十一)——SpringMVC实现Resultful服务
    (转)淘淘商城系列——maven工程debug调试
    日常英语---200120(Some penguins are ticklish)
    心得体悟帖---200120(娱乐和工作同时进行)(觉得有用才会做)
    日常英语---200119(fall)
    日常英语---200119(American Cancer Society Reports Largest Ever Drop in US Cancer Deaths)
    心得体悟帖---200119(不同的选择有不同的烦恼)
    心得体悟帖---200119(玩游戏是正常的,及时制止就好)
  • 原文地址:https://www.cnblogs.com/double-s/p/8117814.html
Copyright © 2020-2023  润新知