一、關於驗證碼
首先來說,驗證碼在我們的日常登錄,發表言論時比較常見,它是一種用來區分登錄用戶是人還是機器的一種公共自動程序。它可以防止惡意破解密碼、刷票、論壇灌水。
二、用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>
當我們需要使用那些效果時,我們可以在web.xml中配置它們。
