整體思路,簡單講,就是後臺生成圖片同時將圖片信息保存在session,前端顯示圖片,輸入驗證碼信息後提交表單到後臺,取出存放在session裏的驗證碼信息,與表單提交的驗證碼信息覈對。前端
點擊驗證碼圖片時,經過jquery從新請求後臺生成驗證碼圖片方法,更換圖片。java
首先在後端controller裏,有這樣一個方法:jquery
路徑爲http://localhost:8888/RiXiang_blog/login/captcha.form,訪問這個路徑即可以經過response寫入圖片。後端
@RequestMapping(value = "/captcha", method = RequestMethod.GET) @ResponseBody public void captcha(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { CaptchaUtil.outputCaptcha(request, response); }
CaptchaUtil是一個工具類,封裝了驗證碼圖片生成,和存儲session功能。session
代碼以下:app
package com.util; 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.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; /** * @ClassName: CaptchaUtil * @Description: 關於驗證碼的工具類 * @author 無名 * @date 2016-5-7 上午8:33:08 * @version 1.0 */ public final class CaptchaUtil { private CaptchaUtil(){} /* * 隨機字符字典 */ private static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; /* * 隨機數 */ private static Random random = new Random(); /* * 獲取6位隨機數 */ private static String getRandomString() { StringBuffer buffer = new StringBuffer(); for(int i = 0; i < 6; i++) { buffer.append(CHARS[random.nextInt(CHARS.length)]); } return buffer.toString(); } /* * 獲取隨機數顏色 */ private static Color getRandomColor() { return new Color(random.nextInt(255),random.nextInt(255), random.nextInt(255)); } /* * 返回某顏色的反色 */ private static Color getReverseColor(Color c) { return new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c.getBlue()); } public static void outputCaptcha(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg"); String randomString = getRandomString(); request.getSession(true).setAttribute("randomString", randomString); int width = 100; int height = 30; Color color = getRandomColor(); Color reverse = getReverseColor(color); BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16)); g.setColor(color); g.fillRect(0, 0, width, height); g.setColor(reverse); g.drawString(randomString, 18, 20); for (int i = 0, n = random.nextInt(100); i < n; i++) { g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1); } // 轉成JPEG格式 ServletOutputStream out = response.getOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(bi); out.flush(); } }
前端獲取驗證碼圖片,要這樣寫:dom
……工具
<tr>
<th>captcha</th>
<td>
<input type="text" id="captcha" name="captcha" class="text" maxlength="10" />
<img id="captchaImage" src="captcha.form"/>
</td>
</tr>spa
img的src裏寫入路徑,頁面加載時就會訪問http://localhost:8888/RiXiang_blog/login/captcha.form獲取圖片。code
表單的提交和登陸信息驗證就不具體講了。
點擊更換驗證碼的js代碼以下:
// 更換驗證碼 $('#captchaImage').click(function() { $('#captchaImage').attr("src", "captcha.form?timestamp=" + (new Date()).valueOf()); });
能夠看到後面加入時間戳做爲參數,timestamp=" + (new Date()).valueOf()。加入這個參數就能夠實現從新訪問後臺方法。不然是沒法刷新圖像的。