Java生成驗證碼
Java 生成驗證碼的流程是:
收到請求->生成驗證碼所用的隨機數(shù)->使用隨機數(shù)寫出圖片->將隨機數(shù)記錄到Session中->輸出驗證碼
Java 驗證驗證碼的流程是:
收到請求->獲取用戶傳過來的驗證碼數(shù)字->驗證是否正確->輸出驗證結(jié)果
下面通過一個例子來展示驗證碼的生成流程,該例子使用基本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;// 定義圖片上顯示驗證碼的個數(shù) 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(); // 創(chuàng)建一個隨機數(shù)生成器類 Random random = new Random(); // 將圖像填充為白色 gd.setColor(Color.WHITE); gd.fillRect(0, 0, width, height); // 創(chuàng)建字體,字體的大小應該根據(jù)圖片的高度來定。 Font font = new Font("Fixedsys", Font.BOLD, fontHeight); // 設置字體。 gd.setFont(font); // 畫邊框。 gd.setColor(Color.BLACK); gd.drawRect(0, 0, width - 1, height - 1); // 隨機產(chǎn)生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用于保存隨機產(chǎn)生的驗證碼,以便用戶登錄后進行驗證。 StringBuffer randomCode = new StringBuffer(); int red = 0, green = 0, blue = 0; // 隨機產(chǎn)生codeCount數(shù)字的驗證碼。 for (int i = 0; i < codeCount; i++) { // 得到隨機產(chǎn)生的驗證碼數(shù)字。 String code = String.valueOf(codeSequence[random.nextInt(36)]); // 產(chǎn)生隨機的顏色分量來構(gòu)造顏色值,這樣輸出的每位數(shù)字的顏色值都將不同。 red = random.nextInt(255); green = random.nextInt(255); blue = random.nextInt(255); // 用隨機產(chǎn)生的顏色將驗證碼繪制到圖像中。 gd.setColor(new Color(red, green, blue)); gd.drawString(code, (i + 1) * xx, codeY); // 將產(chǎn)生的四個隨機數(shù)組合在一起。 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); } }); }
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關文章
springboot中實現(xiàn)通過后臺創(chuàng)建臨時表
這篇文章主要介紹了springboot中實現(xiàn)通過后臺創(chuàng)建臨時表操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07