大家都知道為了防止我們的網站被有些人和黑客惡意攻擊,比如我們網站的注冊頁面,如果我們在用戶注冊的時候不加上一個驗證碼框的話,別人就可以寫一個腳本對你的網站進行惡意的注冊,比如每分鍾對你的網站進行n次的注冊,那么你的網站就會被攻擊而崩潰。當我們增加了驗證碼之后,別人再寫腳本的時候就必須先識別你的驗證碼,而要識別圖片驗證碼中的內容,卻不是那么的容易,這樣就能夠有效的防止我們的網站被惡意的注冊攻擊。廢話不多說,直接上代碼。
生成驗證碼和驗證碼圖片的工具類
1 package com.utils;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Graphics;
6 import java.awt.image.BufferedImage;
7 import java.awt.image.RenderedImage;
8 import java.io.FileOutputStream;
9 import java.io.OutputStream;
10 import java.util.HashMap;
11 import java.util.Map;
12 import java.util.Random;
13
14 import javax.imageio.ImageIO;
15
16 public class CodeUtil {
17 private static int width = 90;// 定義圖片的width
18 private static int height = 20;// 定義圖片的height
19 private static int codeCount = 4;// 定義圖片上顯示驗證碼的個數
20 private static int xx = 15;
21 private static int fontHeight = 18;
22 private static int codeY = 16;
23 private static char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
24 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
25
26 /**
27 * 生成一個map集合
28 * code為生成的驗證碼
29 * codePic為生成的驗證碼BufferedImage對象
30 * @return
31 */
32 public static Map<String,Object> generateCodeAndPic() {
33 // 定義圖像buffer
34 BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
35 // Graphics2D gd = buffImg.createGraphics();
36 // Graphics2D gd = (Graphics2D) buffImg.getGraphics();
37 Graphics gd = buffImg.getGraphics();
38 // 創建一個隨機數生成器類
39 Random random = new Random();
40 // 將圖像填充為白色
41 gd.setColor(Color.WHITE);
42 gd.fillRect(0, 0, width, height);
43
44 // 創建字體,字體的大小應該根據圖片的高度來定。
45 Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
46 // 設置字體。
47 gd.setFont(font);
48
49 // 畫邊框。
50 gd.setColor(Color.BLACK);
51 gd.drawRect(0, 0, width - 1, height - 1);
52
53 // 隨機產生40條干擾線,使圖象中的認證碼不易被其它程序探測到。
54 gd.setColor(Color.BLACK);
55 for (int i = 0; i < 30; i++) {
56 int x = random.nextInt(width);
57 int y = random.nextInt(height);
58 int xl = random.nextInt(12);
59 int yl = random.nextInt(12);
60 gd.drawLine(x, y, x + xl, y + yl);
61 }
62
63 // randomCode用於保存隨機產生的驗證碼,以便用戶登錄后進行驗證。
64 StringBuffer randomCode = new StringBuffer();
65 int red = 0, green = 0, blue = 0;
66
67 // 隨機產生codeCount數字的驗證碼。
68 for (int i = 0; i < codeCount; i++) {
69 // 得到隨機產生的驗證碼數字。
70 String code = String.valueOf(codeSequence[random.nextInt(36)]);
71 // 產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。
72 red = random.nextInt(255);
73 green = random.nextInt(255);
74 blue = random.nextInt(255);
75
76 // 用隨機產生的顏色將驗證碼繪制到圖像中。
77 gd.setColor(new Color(red, green, blue));
78 gd.drawString(code, (i + 1) * xx, codeY);
79
80 // 將產生的四個隨機數組合在一起。
81 randomCode.append(code);
82 }
83 Map<String,Object> map =new HashMap<String,Object>();
84 //存放驗證碼
85 map.put("code", randomCode);
86 //存放生成的驗證碼BufferedImage對象
87 map.put("codePic", buffImg);
88 return map;
89 }
90
91 public static void main(String[] args) throws Exception {
92 //創建文件輸出流對象
93 OutputStream out = new FileOutputStream("D://img/"+System.currentTimeMillis()+".jpg");
94 Map<String,Object> map = CodeUtil.generateCodeAndPic();
95 ImageIO.write((RenderedImage) map.get("codePic"), "jpeg", out);
96 System.out.println("驗證碼的值為:"+map.get("code"));
97 }
98 }
接下來是一個以jsp、servlet的應用demo
1.jsp頁面代碼
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>驗證碼頁面</title>
8 <script type="text/javascript"
9 src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
10 </head>
11 <body>
12 <form action="${pageContext.request.contextPath}/checkCode" method="post">
13 請輸入驗證碼:<input type="text" name="code" style="width: 80px;" /> <img id="imgObj" alt="驗證碼"
14 src="${pageContext.request.contextPath}/getCode"><a href="#" onclick="changeImg()">換一張</a><br/> <input
15 type="submit" value="提交" />
16 </form>
17
18 </body>
19 <script type="text/javascript">
20 $(function() {
21
22 });
23
24 function changeImg() {
25 var imgSrc = $("#imgObj");
26 var src = imgSrc.attr("src");
27 imgSrc.attr("src", chgUrl(src));
28 }
29
30 // 時間戳
31 // 為了使每次生成圖片不一致,即不讓瀏覽器讀緩存,所以需要加上時間戳
32 function chgUrl(url) {
33 var timestamp = (new Date()).valueOf();
34 url = url.substring(0, 20);
35 if ((url.indexOf("&") >= 0)) {
36 url = url + "×tamp=" + timestamp;
37 } else {
38 url = url + "?timestamp=" + timestamp;
39 }
40 return url;
41 }
42
43 </script>
44 </html>
2.后台產生驗證碼的servlet
package com.code;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.utils.CodeUtil;
/**
* Servlet implementation class CodeServlet
*/
@WebServlet("/getCode")
public class CodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 調用工具類生成的驗證碼和驗證碼圖片
Map<String, Object> codeMap = CodeUtil.generateCodeAndPic();
// 將四位數字的驗證碼保存到Session中。
HttpSession session = req.getSession();
session.setAttribute("code", codeMap.get("code").toString());
// 禁止圖像緩存。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", -1);
resp.setContentType("image/jpeg");
// 將圖像輸出到Servlet輸出流中。
ServletOutputStream sos;
try {
sos = resp.getOutputStream();
ImageIO.write((RenderedImage) codeMap.get("codePic"), "jpeg", sos);
sos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.校驗驗證碼的servlet
package com.code;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/checkCode")
public class CheckCode extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String code = request.getParameter("code");
// 驗證驗證碼
String sessionCode = request.getSession().getAttribute("code").toString();
if (code != null && !"".equals(code) && sessionCode != null && !"".equals(sessionCode)) {
if (code.equalsIgnoreCase(sessionCode)) {
response.getWriter().println("驗證通過!");
} else {
response.getWriter().println("驗證失敗!");
}
} else {
response.getWriter().println("驗證失敗!");
}
}
}
后端直接寫一個流給前端,前端直接使用<img id="imgObj" alt="驗證碼"
src="${pageContext.request.contextPath}/getCode">就可以了

