用java实现验证码(CAPTCHA)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

⽤java实现验证码(CAPTCHA)
⼀、关于验证码
⾸先来说,验证码在我们的⽇常登录,发表⾔论时⽐较常见,它是⼀种⽤来区分登录⽤户是⼈还是机器的⼀种公共⾃动程序。

它可以防⽌恶意破解密码、刷票、论坛灌⽔。

⼆、⽤java来实现验证码
了解了验证码的作⽤,接下来我们就⽤java来实现验证码这⼀功能。

⾸先我们先分析⼀下,怎么⽣成⼀个验证码。

我们需要⼀个BufferedImage对象来保存图⽚;通过它来获得Graphics对象;通过Randrom产⽣随机验证码信息;使⽤Graphics绘制图⽚;记录验证码信息到session中;使⽤ImageIO输出图⽚;
1package com.water.servlet;
2
3import com.sun.javafx.font.FontStrike;
4
5import javax.imageio.ImageIO;
6import javax.servlet.ServletException;
7import javax.servlet.http.HttpServlet;
8import javax.servlet.http.HttpServletRequest;
9import javax.servlet.http.HttpServletResponse;
10import java.awt.*;
11import java.awt.image.BufferedImage;
12import java.io.IOException;
13import java.util.Random;
14
15public class ImgServlet extends HttpServlet {
16 @Override
17protected 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);
23char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
24 StringBuffer sb = new StringBuffer();
25 Random random = new Random();
26for (int i = 0; i < 4; i++) {
27char 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>
6function reloadCode(){
7var 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. 使⽤之前要先下载(提取密码:1vrf)
b. 然后是在web.xml中配置它
1<servlet>
2<servlet-name>Kaptcha</servlet-name>
3<servlet-class>.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="/xml/ns/j2ee"
3 xmlns:xsi="/2001/XMLSchema-instance"
4 xsi:schemaLocation="/xml/ns/j2ee
5 /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>s</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中配置它们。

相关文档
最新文档