⽤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 = Int(36)];
28            graphics.setColor(new Int(88),Int(188),Int(255)));
29            graphics.drawString(aChar+"",(i*15)+3,18);
30            sb.append(aChar);
31        }
32        Session().setAttribute("charCode",sb.toString());
33        ImageIO.write(bi,"jpg",OutputStream());
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            ElementById("picCode").src = "<%=ContextPath()%>/imgServlet?d="+time;
9        }
10</script>
11</head>
12<body>
13<form action="<%=ContextPath()%>/loginServlet" method="post">
14验证码:<input type="text" name="checkCode"/>
15<img id="picCode" src="<%=ContextPath()%>/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. 然后是在l中配置它
1<servlet>
2<servlet-name>Kaptcha</servlet-name>
3<servlet-class&le.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="java.sun/xml/ns/j2ee"
3    xmlns:xsi="/2001/XMLSchema-instance"
4    xsi:schemaLocation="java.sun/xml/ns/j2ee
5    java.sun/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>lor</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                de.kaptcha.impl.DefaultKaptcha
45</param-value>
46</init-param>
47<init-param>
48<description>⽂本实现类</description>
49<param-name&producer.impl</param-name>
50<param-value>
51                impl.DefaultTextCreator
52</param-value>
53</init-param>
54<init-param>
55<description>⽂本集合,验证码值从此集合中获取</description>
56<param-name&producer.char.string</param-name>
57<param-value>1234567890</param-value>
58</init-param>
59<init-param>
60<description>验证码长度 5</description>
61<param-name&producer.char.length</param-name>
62<param-value>2</param-value>
63</init-param>
64<init-param>
html怎么实现登录验证功能
65<description>字体 Arial, Courier</description>
66<param-name&producer.font.names</param-name> 67<param-value>Arial, Courier</param-value>
68</init-param>
69<init-param>
70<description>字体⼤⼩ 40px.</description>
71<param-name&producer.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&lor</param-name> 79<param-value>black</param-value>
80</init-param>
81<init-param>
82<description>⽂字间隔 2</description>
83<param-name&producer.char.space</param-name> 84<param-value>2</param-value>
85</init-param>
86<init-param>
87<description>⼲扰实现类</description>
88<param-name&ise.impl</param-name>
89<param-value>
90<!-- de.kaptcha.impl.NoNoise -->
91                de.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&lor</param-name>
99<param-value>black</param-value>
100</init-param>
101<init-param>
102<description>
103图⽚样式:⽔纹de.kaptcha.impl.WaterRipple 104鱼眼de.kaptcha.impl.FishEyeGimpy
105阴影de.kaptcha.impl.ShadowGimpy
106</description>
107<param-name>kaptcha.obscurificator.impl</param-name>
108<param-value>
109                de.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                de.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.</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                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
当我们需要使⽤那些效果时,我们可以在l中配置它们。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。