欧美日韩不卡一区二区三区,www.蜜臀.com,高清国产一区二区三区四区五区,欧美日韩三级视频,欧美性综合,精品国产91久久久久久,99a精品视频在线观看

java語言

java如何生成驗證碼

時間:2025-05-02 09:10:01 java語言 我要投稿

java如何生成驗證碼

  在程序開發(fā)中往往會遇到為了防止別人惡意登陸而設置驗證碼,驗證碼是在程序界普遍應用很多,所以用戶對驗證碼也很熟悉。以下是小編為大家搜索整理java如何生成驗證碼,希望能給大家?guī)韼椭?更多精彩內容請及時關注我們應屆畢業(yè)生考試網!

  Java生成驗證碼的流程是:

  收到請求->生成驗證碼所用的隨機數(shù)->使用隨機數(shù)寫出圖片->將隨機數(shù)記錄到Session中->輸出驗證碼

  Java驗證驗證碼的流程是:

  收到請求->獲取用戶傳過來的驗證碼數(shù)字->驗證是否正確->輸出驗證結果

  下面通過一個例子來展示驗證碼的生成流程,該例子使用基本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;// 定義圖片的widthprivate int height = 20;// 定義圖片的heightprivate 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' };

  66public 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)建字體,字體的大小應該根據圖片的高度來定。 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數(shù)字的驗證碼。 for (int i = 0; i < codeCount; i++) { // 得到隨機產生的驗證碼數(shù)字。 String code = String.valueOf(codeSequence[random.nextInt(36)]); // 產生隨機的顏色分量來構造顏色值,這樣輸出的每位數(shù)字的顏色值都將不同。 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); // 將產生的四個隨機數(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.接收用戶傳過來的驗證碼并驗證:

  21/*** 驗證驗證碼*/@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; }}

  頁面請求驗證碼并驗證的例子:

-請求驗證碼:

  -驗證驗證碼:

  17function 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); }});}

【java如何生成驗證碼】相關文章:

java如何生成驗證碼呢05-14

java中全排列是如何生成算法05-18

用php生成帶有雪花背景的驗證碼01-08

php生成動態(tài)圖片驗證碼代碼07-23

php如何實現(xiàn)驗證碼06-13

JAVA實現(xiàn)生成GUID的方法06-02

php生成動態(tài)圖片驗證碼的一段代碼04-30

java如何構造03-02

如何使用java05-27