用java实现验证码(CAPTCHA)

    一、关于验证码

    首先来说,验证码在我们的日常登录,发表言论时比较常见,它是一种用来区分登录用户是人还是机器的一种公共自动程序。它可以防止恶意破解密码、刷票、论坛灌水。

    二、用java来实现验证码

    了解了验证码的作用,接下来我们就用java来实现验证码这一功能。首先我们先分析一下,怎么生成一个验证码。我们需要一个BufferedImage对象来保存图片;通过它来获得Graphics对象;通过Randrom产生随机验证码信息;使用Graphics绘制图片;记录验证码信息到session中;使用ImageIO输出图片;

 1 package com.water.servlet;
 2 
 3 import com.sun.javafx.font.FontStrike;
 4 
 5 import javax.imageio.ImageIO;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 import java.awt.*;
11 import java.awt.image.BufferedImage;
12 import java.io.IOException;
13 import java.util.Random;
14 
15 public class ImgServlet extends HttpServlet {
16     @Override
17     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
18         BufferedImage bi = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);
19         Graphics graphics = bi.getGraphics();
20         Color color = new Color(200, 150, 255);
21         graphics.setColor(color);
22         graphics.fillRect(0,0,68,32);
23         char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
24         StringBuffer sb = new StringBuffer();
25         Random random = new Random();
26         for (int i = 0; i < 4; i++) {
27             char aChar = chars[random.nextInt(36)];
28             graphics.setColor(new Color(random.nextInt(88),random.nextInt(188),random.nextInt(255)));
29             graphics.drawString(aChar+"",(i*15)+3,18);
30             sb.append(aChar);
31         }
32         req.getSession().setAttribute("charCode",sb.toString());
33         ImageIO.write(bi,"jpg",resp.getOutputStream());
34 
35     }
36 }

    写一个 登录页面来检验验证码的正确与否。

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3   <head>
 4     <title>checkCode</title>
 5     <script>
 6         function reloadCode(){
 7             var time = new Date().getTime();//为了防止缓存导致不能正常刷新验证码
 8             document.getElementById("picCode").src = "<%=request.getContextPath()%>/imgServlet?d="+time;
 9         }
10     </script>
11   </head>
12   <body>
13   <form action="<%=request.getContextPath()%>/loginServlet" method="post">
14     验证码:<input type="text" name="checkCode" />
15     <img id="picCode" src="<%=request.getContextPath()%>/imgServlet" />
16     <a href="javascript:reloadCode();">看不清楚</a>
17     <input type="submit" value="Checked" />
18   </form>
19   </body>
20 
21 </html>

    通过验证,我们发现验证码可以正确显示并能实现验证功能。但是在日常中,有着各种各样的验证码:算术类型,文字类型,动画类型,纯数字类型,数字与文字、字母相结合类型等等,那么我们在开发中该怎么实现这些功能呢?

    三、 验证码组件

    我们能够走的更远更扎实,是因为我们站在巨人的肩旁上。我们的前辈们,已经开发了一些很好的组件可供我们使用,比如下面这些:Jcaptcha组件和Kaptcha组件。由于Jcaptcha组件需要和Spring框架结合来使用,我们就先咱不做介绍,我们主要来说一说Kaptcha组件。

    a. 使用之前要先下载Kaptcha组件(提取密码:1vrf)

    b. 然后是在web.xml中配置它

1 <servlet>
2     <servlet-name>Kaptcha</servlet-name>
3     <servlet-class>com.google.com.kaptcha.servlet.KaptchaServlet</servlet-class>
4 </servlet>
5 <servlet-mapping>
6    <servlet-name>Kaptcha</servlet-name>
7    <url-pattern>/randromCode.jpg</url-pattern>
8 </servlet-mapping>

    Tips:Kaptcha组件还有很多配置选项供我们使用。

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
  3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  5     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6 
  7     <servlet>
  8         <servlet-name>Kaptcha</servlet-name>
  9         <servlet-class>
 10             KaptchaServlet
 11         </servlet-class>
 12         <init-param>
 13             <description>图片边框,合法值:yes , no</description>
 14             <param-name>kaptcha.border</param-name>
 15             <param-value>yes</param-value>
 16         </init-param>
 17         <init-param>
 18             <description>
 19                 边框颜色,合法值: r,g,b (and optional alpha) 或者
 20                 white,black,blue.
 21             </description>
 22             <param-name>kaptcha.border.color</param-name>
 23             <param-value>black</param-value>
 24         </init-param>
 25         <init-param>
 26             <description>边框厚度,合法值:>0</description>
 27             <param-name>kaptcha.border.thickness</param-name>
 28             <param-value>1</param-value>
 29         </init-param>
 30         <init-param>
 31             <description>图片宽 200</description>
 32             <param-name>kaptcha.image.width</param-name>
 33             <param-value>200</param-value>
 34         </init-param>
 35         <init-param>
 36             <description>图片高 50</description>
 37             <param-name>kaptcha.image.height</param-name>
 38             <param-value>50</param-value>
 39         </init-param>
 40         <init-param>
 41             <description>图片实现类</description>
 42             <param-name>kaptcha.producer.impl</param-name>
 43             <param-value>
 44                 com.google.code.kaptcha.impl.DefaultKaptcha
 45             </param-value>
 46         </init-param>
 47         <init-param>
 48             <description>文本实现类</description>
 49             <param-name>kaptcha.textproducer.impl</param-name>
 50             <param-value>
 51                 com.google.code.kaptcha.text.impl.DefaultTextCreator
 52             </param-value>
 53         </init-param>
 54         <init-param>
 55             <description>文本集合,验证码值从此集合中获取</description>
 56             <param-name>kaptcha.textproducer.char.string</param-name>
 57             <param-value>1234567890</param-value>
 58         </init-param>
 59         <init-param>
 60             <description>验证码长度 5</description>
 61             <param-name>kaptcha.textproducer.char.length</param-name>
 62             <param-value>2</param-value>
 63         </init-param>
 64         <init-param>
 65             <description>字体 Arial, Courier</description>
 66             <param-name>kaptcha.textproducer.font.names</param-name>
 67             <param-value>Arial, Courier</param-value>
 68         </init-param>
 69         <init-param>
 70             <description>字体大小 40px.</description>
 71             <param-name>kaptcha.textproducer.font.size</param-name>
 72             <param-value>40</param-value>
 73         </init-param>
 74         <init-param>
 75             <description>
 76                 字体颜色,合法值: r,g,b 或者 white,black,blue.
 77             </description>
 78             <param-name>kaptcha.textproducer.font.color</param-name>
 79             <param-value>black</param-value>
 80         </init-param>
 81         <init-param>
 82             <description>文字间隔 2</description>
 83             <param-name>kaptcha.textproducer.char.space</param-name>
 84             <param-value>2</param-value>
 85         </init-param>
 86         <init-param>
 87             <description>干扰实现类</description>
 88             <param-name>kaptcha.noise.impl</param-name>
 89             <param-value>
 90                 <!-- com.google.code.kaptcha.impl.NoNoise -->
 91                 com.google.code.kaptcha.impl.DefaultNoise
 92             </param-value>
 93         </init-param>
 94         <init-param>
 95             <description>
 96                 干扰颜色,合法值: r,g,b 或者 white,black,blue.
 97             </description>
 98             <param-name>kaptcha.noise.color</param-name>
 99             <param-value>black</param-value>
100         </init-param>
101         <init-param>
102             <description>
103                 图片样式: 水纹com.google.code.kaptcha.impl.WaterRipple
104                 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
105                 阴影com.google.code.kaptcha.impl.ShadowGimpy
106             </description>
107             <param-name>kaptcha.obscurificator.impl</param-name>
108             <param-value>
109                 com.google.code.kaptcha.impl.WaterRipple
110             </param-value>
111         </init-param>
112         <init-param>
113             <description>背景实现类</description>
114             <param-name>kaptcha.background.impl</param-name>
115             <param-value>
116                 com.google.code.kaptcha.impl.DefaultBackground
117             </param-value>
118         </init-param>
119         <init-param>
120             <description>背景颜色渐变,开始颜色</description>
121             <param-name>kaptcha.background.clear.from</param-name>
122             <param-value>green</param-value>
123         </init-param>
124         <init-param>
125             <description>背景颜色渐变,结束颜色</description>
126             <param-name>kaptcha.background.clear.to</param-name>
127             <param-value>white</param-value>
128         </init-param>
129         <init-param>
130             <description>文字渲染器</description>
131             <param-name>kaptcha.word.impl</param-name>
132             <param-value>
133                 com.google.code.kaptcha.text.impl.DefaultWordRenderer
134             </param-value>
135         </init-param>
136         <init-param>
137             <description>
138                 session中存放验证码的key键
139             </description>
140             <param-name>kaptcha.session.key</param-name>
141             <param-value>KAPTCHA_SESSION_KEY</param-value>
142         </init-param>
143         <init-param>
144             <description>
145                 The date the kaptcha is generated is put into the
146                 HttpSession. This is the key value for that item in the
147                 session.
148             </description>
149             <param-name>kaptcha.session.date</param-name>
150             <param-value>KAPTCHA_SESSION_DATE</param-value>
151         </init-param>
152     </servlet>
153     <servlet-mapping>
154         <servlet-name>Kaptcha</servlet-name>
155         <url-pattern>/randomcode.jpg</url-pattern>
156     </servlet-mapping>
157 
158 </web-app>
View Code

    当我们需要使用那些效果时,我们可以在web.xml中配置它们。

 

posted @ 2017-08-15 01:29  守明  阅读(5398)  评论(0编辑  收藏  举报