用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>checkCodetitle>
 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>Kaptchaservlet-name>
3     <servlet-class>com.google.com.kaptcha.servlet.KaptchaServletservlet-class>
4 servlet>
5 <servlet-mapping>
6    <servlet-name>Kaptchaservlet-name>
7    <url-pattern>/randromCode.jpgurl-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>Kaptchaservlet-name>
  9         <servlet-class>
 10             KaptchaServlet
 11         servlet-class>
 12         <init-param>
 13             <description>图片边框,合法值:yes , nodescription>
 14             <param-name>kaptcha.borderparam-name>
 15             <param-value>yesparam-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.colorparam-name>
 23             <param-value>blackparam-value>
 24         init-param>
 25         <init-param>
 26             <description>边框厚度,合法值:>0description>
 27             <param-name>kaptcha.border.thicknessparam-name>
 28             <param-value>1param-value>
 29         init-param>
 30         <init-param>
 31             <description>图片宽 200description>
 32             <param-name>kaptcha.image.widthparam-name>
 33             <param-value>200param-value>
 34         init-param>
 35         <init-param>
 36             <description>图片高 50description>
 37             <param-name>kaptcha.image.heightparam-name>
 38             <param-value>50param-value>
 39         init-param>
 40         <init-param>
 41             <description>图片实现类description>
 42             <param-name>kaptcha.producer.implparam-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.implparam-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.stringparam-name>
 57             <param-value>1234567890param-value>
 58         init-param>
 59         <init-param>
 60             <description>验证码长度 5description>
 61             <param-name>kaptcha.textproducer.char.lengthparam-name>
 62             <param-value>2param-value>
 63         init-param>
 64         <init-param>
 65             <description>字体 Arial, Courierdescription>
 66             <param-name>kaptcha.textproducer.font.namesparam-name>
 67             <param-value>Arial, Courierparam-value>
 68         init-param>
 69         <init-param>
 70             <description>字体大小 40px.description>
 71             <param-name>kaptcha.textproducer.font.sizeparam-name>
 72             <param-value>40param-value>
 73         init-param>
 74         <init-param>
 75             <description>
 76                 字体颜色,合法值: r,g,b 或者 white,black,blue.
 77             description>
 78             <param-name>kaptcha.textproducer.font.colorparam-name>
 79             <param-value>blackparam-value>
 80         init-param>
 81         <init-param>
 82             <description>文字间隔 2description>
 83             <param-name>kaptcha.textproducer.char.spaceparam-name>
 84             <param-value>2param-value>
 85         init-param>
 86         <init-param>
 87             <description>干扰实现类description>
 88             <param-name>kaptcha.noise.implparam-name>
 89             <param-value>
 90                 
 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.colorparam-name>
 99             <param-value>blackparam-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.implparam-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.implparam-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.fromparam-name>
122             <param-value>greenparam-value>
123         init-param>
124         <init-param>
125             <description>背景颜色渐变,结束颜色description>
126             <param-name>kaptcha.background.clear.toparam-name>
127             <param-value>whiteparam-value>
128         init-param>
129         <init-param>
130             <description>文字渲染器description>
131             <param-name>kaptcha.word.implparam-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.keyparam-name>
141             <param-value>KAPTCHA_SESSION_KEYparam-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.dateparam-name>
150             <param-value>KAPTCHA_SESSION_DATEparam-value>
151         init-param>
152     servlet>
153     <servlet-mapping>
154         <servlet-name>Kaptchaservlet-name>
155         <url-pattern>/randomcode.jpgurl-pattern>
156     servlet-mapping>
157 
158 web-app>
View Code

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

 

转载于:https://www.cnblogs.com/shouming/articles/7362344.html

你可能感兴趣的:(用java实现验证码(CAPTCHA))