http://blog.csdn.net/ayhlay/article/details/13625275
1.驗(yàn)證碼及其作用
驗(yàn)證碼:就是將一串隨機(jī)產(chǎn)生的數(shù)字或符號,生成一幅圖片拳话, 圖片里加上一些干擾象素(防止OCR)歹茶,由用戶肉眼識別其中的驗(yàn)證碼信息夕玩,輸入表單提交網(wǎng)站驗(yàn)證,驗(yàn)證成功后才能使用某項(xiàng)功能惊豺。
作用:驗(yàn)證碼一般是防止有人利用機(jī)器人自動批量注冊燎孟、對特定的注冊用戶用特定程序暴力破解方式進(jìn)行不斷的登陸、灌水尸昧。因?yàn)轵?yàn)證碼是一個混合了數(shù)字或符號的圖片揩页,人眼看起來都費(fèi)勁,機(jī)器識別起來就更困難烹俗。像百度貼吧未登錄發(fā)貼要輸入驗(yàn)證碼大概是防止大規(guī)模匿名回帖的發(fā)生爆侣。
開發(fā)步驟
1、開發(fā)servletpackage com.controller;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class SafeCode extends HttpServlet {public static final char[] CHARS = { '1', '2', '3', '4', '5', '6', '7','8', '9', 'k', 'A', 'Q', 'x', 'E', 'R', 'T', 'G', 'D', 'S', 'W','G', 'H', 'C', 'B', 'a', 'w', 'e', 'r', 't', 'd', 'F' };// 隨機(jī)字符的字典public static Random random = new Random();// 隨機(jī)數(shù)public static String getRandomString() {// 字符的緩存StringBuffer buf = new StringBuffer();for (int i = 0; i <4; i++) {// 循環(huán) 六次buf.append(CHARS[random.nextInt(CHARS.length)]);}return buf.toString();}public static Color getRandomColor() {return new Color(random.nextInt(255), random.nextInt(255),random.nextInt(255));}public static Color getReverseColor(Color c) {return new Color(255 - c.getRed(), 255 - c.getGreen(),255 - c.getBlue());}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("image/jpeg");// 設(shè)置輸出的類型String randomString = getRandomString();// 得到返回的字符集request.getSession(true).setAttribute("randomString", randomString);int with = 80;int hight =36;// 生成圖片的大小Color color = getRandomColor();// 用于背景色Color reverse = getReverseColor(color);// 用于前景色BufferedImage bi = new BufferedImage(with, hight,BufferedImage.TYPE_INT_RGB);// 創(chuàng)建一個彩色的圖片Graphics2D g = bi.createGraphics();// 獲取到繪圖對象g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 18));// 設(shè)置字體g.setColor(color);// 設(shè)置顏色g.fillRect(0, 0, with, hight);// 繪制背景g.setColor(reverse);// 設(shè)置顏色g.drawString(randomString, 18, 25);// 繪制隨機(jī)字符for (int i = 0, n = random.nextInt(18); i < n; i++) {// 畫最多100個噪音點(diǎn)g.drawRect(random.nextInt(with), random.nextInt(hight), 1, 1);// 隨機(jī)噪音點(diǎn)}ServletOutputStream out = response.getOutputStream();// 轉(zhuǎn)換圖片格式JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(bi);// 對圖片進(jìn)行編碼out.flush();// 輸出}protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doGet(req, resp);}}
2幢妄、在jsp頁面中開發(fā)<input id="yzm"
name="yzm" type="text" class="form-control x164 in"><img alt="驗(yàn)證碼" src="SafeCode" id="Identity">
請?zhí)顚憟D片中的字符兔仰,不區(qū)分大小寫<a href="javascript:go()">看不清楚?換張圖片</a><script type="text/javascript">function go(){
document.getElementById("Identity").src = 'SafeCode?dddd='
+ new Date().getTime();</script>
}3開發(fā)servletString jl = request.getParameter("jl");String yzm = request.getParameter("yzm");String randomString = (String)request.getSession().getAttribute("randomString");
if(yzm.equalsIgnoreCase(randomString)){out.print("驗(yàn)證通過:"+user+"
"+jl);}else{out.print("驗(yàn)證非法=对А:醺啊!潮尝!");}