Java 生成驗證碼的流程是:
收到請求->生成驗證碼所用的隨機數->使用隨機數寫出圖片->將隨機數記錄到Session中->輸出驗證碼
Java 驗證驗證碼的流程是:
收到請求->獲取用戶傳過來的驗證碼數字->驗證是否正確->輸出驗證結果
下面通過一個例子來展示驗證碼的生成流程,該例子使用基本Java Spring框架的Rest接口,可以使用任何平臺來獲取驗證碼:
服務器處理驗證碼的例子:
1.接收驗證碼請求:
/** * 接收驗證碼請求 */ @RequestMapping(value="captchacode") public void CaptchaCode(){ try { CaptchaCodeModel captchaCodeModel=new CaptchaCode().getCode(); //將驗證碼放到Session中 HttpServletRequest httpServletRequest=super.getRequest(); httpServletRequest.getSession().setAttribute("captchacodekey", captchaCodeModel.getCaptchaCode()); //將圖片寫到客戶端 HttpServletResponse httpServletResponse=super.getResponse(); //禁止緩存 httpServletResponse.setHeader("Pragma", "no-cache"); httpServletResponse.setHeader("Cache-Control", "no-cache"); httpServletResponse.setDateHeader("Expires", 0); ServletOutputStream servletOutputStream=httpServletResponse.getOutputStream(); //輸出圖片 ImageIO.write(captchaCodeModel.getCaptchaImage(), "jpeg", servletOutputStream); servletOutputStream.close(); } catch (Exception e) { logger.info("驗證碼生成失敗:"+e.getMessage()); } }
2.生成驗證碼并生成圖片:
public class CaptchaCode { private int width = 90;// 定義圖片的width private int height = 20;// 定義圖片的height private int codeCount = 4;// 定義圖片上顯示驗證碼的個數 private int xx = 15; private int fontHeight = 18; private int codeY = 16; char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
public CaptchaCodeModel getCode() throws IOException { // 定義圖像buffer BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics gd = buffImg.getGraphics(); // 創建一個隨機數生成器類 Random random = new Random(); // 將圖像填充為白色 gd.setColor(Color.WHITE); gd.fillRect(0, 0, width, height); // 創建字體,字體的大小應該根據圖片的高度來定。 Font font = new Font("Fixedsys", Font.BOLD, fontHeight); // 設置字體。 gd.setFont(font); // 畫邊框。 gd.setColor(Color.BLACK); gd.drawRect(0, 0, width - 1, height - 1); // 隨機產生40條干擾線,使圖象中的認證碼不易被其它程序探測到。 gd.setColor(Color.BLACK); for (int i = 0; i < 40; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); gd.drawLine(x, y, x + xl, y + yl); } // randomCode用于保存隨機產生的驗證碼,以便用戶登錄后進行驗證。 StringBuffer randomCode = new StringBuffer(); int red = 0, green = 0, blue = 0; // 隨機產生codeCount數字的驗證碼。 for (int i = 0; i < codeCount; i++) { // 得到隨機產生的驗證碼數字。 String code = String.valueOf(codeSequence[random.nextInt(36)]); // 產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。 red = random.nextInt(255); green = random.nextInt(255); blue = random.nextInt(255); // 用隨機產生的顏色將驗證碼繪制到圖像中。 gd.setColor(new Color(red, green, blue)); gd.drawString(code, (i + 1) * xx, codeY); // 將產生的四個隨機數組合在一起。 randomCode.append(code); } CaptchaCodeModel captchaCodeModel=new CaptchaCodeModel(); captchaCodeModel.setCaptchaCode(randomCode.toString()); captchaCodeModel.setCaptchaImage(buffImg); return captchaCodeModel; } public class CaptchaCodeModel{ //驗證碼的String形式 private String captchaCode; //驗證碼的圖片形式 private BufferedImage captchaImage; public String getCaptchaCode() { return captchaCode; } public void setCaptchaCode(String captchaCode) { this.captchaCode = captchaCode; } public BufferedImage getCaptchaImage() { return captchaImage; } public void setCaptchaImage(BufferedImage captchaImage) { this.captchaImage = captchaImage; } }
3.接收用戶傳過來的驗證碼并驗證:
/** * 驗證驗證碼 */ @RequestMapping(value = "valicatpcha") public void register_R() { PageData pageData = super.getPageData(); // 獲取驗證碼 String captchaCode = pageData.getString("captchacode"); HttpServletRequest httpServletRequest = super.getRequest(); Object codeObject = httpServletRequest.getSession().getAttribute(“captchacodekey”); // 驗證碼錯誤 if (codeObject == null || Tools.isEmptyString(captchaCode) || !String.valueOf(codeObject).toUpperCase() .equals(captchaCode.toUpperCase())) { setResult( MessageManager.getInstance().getMessage("invalidcaptcha"), ResultType.Error); return; } }
頁面請求驗證碼并驗證的例子:
-請求驗證碼:<img src='captchacode' style='height:32px;width:148px;'
-驗證驗證碼:
function validcaptchacode(captchaCode) { $.ajax({ type : "POST", url : "valicatpcha", data : { captchacode : captchaCode, tm : new Date().getTime() }, dataType : "json", cache : false, success : function(data) { alert(data); }, error : function(data) { alert(data); } }); }
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。