1 /**
2 * 生成驗(yàn)證碼3 * 改造生成驗(yàn)證碼的方式,將圖片base64形式傳到前臺(tái),而不是直接傳驗(yàn)證碼到前臺(tái)4 *@return
5 *@throwsIOException6 */
7 public void imageCode() throwsIOException {8 HttpServletResponse resp =CommandContext.getResponse();9 HttpServletRequest req =CommandContext.getRequest();10 String method=req.getMethod();11 if(“OPTIONS”.equals(method)){12 return;13 }14 Map map=newHashMap();15
17 int width = 65, height = 38;18 BufferedImage image = newBufferedImage(width, height,19 BufferedImage.TYPE_INT_RGB);20 //獲取圖形上下文
21 Graphics g =image.getGraphics();22 //生成隨機(jī)類
23 Random random = newRandom();24 //設(shè)定背景色
25 g.setColor(getRandColor(230, 255));26 g.fillRect(0, 0, 100, 40);27 //設(shè)定字體
28 g.setFont(new Font(“Arial”, Font.CENTER_BASELINE | Font.ITALIC, 20));29 //產(chǎn)生0條干擾線,
30 g.drawLine(0, 0, 0, 0);31
32 //存放驗(yàn)證碼
33 StringBuffer sRand = newStringBuffer();34 for (int i = 0; i < charCount; i++) {35 String singleCode =String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);36 sRand.append(singleCode);37 //將認(rèn)證碼顯示到圖象中
38 g.setColor(getRandColor(100, 150));//調(diào)用函數(shù)出來(lái)的顏色相同,可能是因?yàn)榉N子太接近,所以只能直接生成
39 g.drawString(singleCode, 14 * i + 5, 25);40 }41 for(int i=0;i
47 HttpSession session =req.getSession();48 //獲取clientid
49 String clientId=SystemUtil.getClientId(req);50 if(StringUtils.isEmpty(clientId)){51 //生成clientid
52 String userAgent=req.getHeader(“User-Agent”);53 String sessionId=session.getId();54 String cip=IpPolicy.getClientIP(req);55 clientId=CodeUtil.genClientId(sessionId,cip,userAgent);56 }57 map.put(“clientId”, clientId);58 if(isValidateCodeCaseSensitive) {59 session.setAttribute(“randomCode”, sRand.toString());60 SystemUtil.push2Cache(clientId, sRand.toString());61 } else{62 session.setAttribute(“randomCode”, sRand.toString().toLowerCase());63 SystemUtil.push2Cache(clientId, sRand.toString().toLowerCase());64 }65 //圖象生效
66 g.dispose();67 try{68
69 ByteArrayOutputStream outputStream = newByteArrayOutputStream();70 ImageIO.write(image, “jpg”, outputStream);71 BASE64Encoder encoder = newBASE64Encoder();72 String base64Img =encoder.encode(outputStream.toByteArray());73 base64Img=”data:image/jpg;base64, “+base64Img.replaceAll(“”, “”).replaceAll(“r”, “”);//刪除 r;
74 map.put(“verCode”, base64Img);75 Object jsonObj =JSONSerializer.toJSON(map);76 byte[] json = jsonObj.toString().getBytes(“UTF-8”);77 resp.setContentType(“text/plain;chartset=utf-8”);78 resp.setHeader(“Cache-Control”, “no-cache”);79 resp.setHeader(“Expires”, “0”);80 resp.setIntHeader(“Content-Length”, json.length);81 ServletOutputStream responseOutputStream =resp.getOutputStream();82 responseOutputStream.write(json);83 //以下關(guān)閉輸入流!
84 responseOutputStream.flush();85 responseOutputStream.close();86 //獲得頁(yè)面key值
87 return;88 } catch(IOException e) {89 logger.error(“生產(chǎn)驗(yàn)證碼出錯(cuò)”,e);90 throw new SystemException(“生產(chǎn)驗(yàn)證碼出錯(cuò)”,e);91 }92 }93
94
95 /**
96 * 給定范圍獲得隨機(jī)顏色97 *98 *@paramfc99 *@parambc100 *@return
101 */
102 Color getRandColor(int fc, intbc) {103 Random random = newRandom();104 if (fc > 255)105 fc = 255;106 if (bc > 255)107 bc = 255;108 int r = fc + random.nextInt(bc -fc);109 int g = fc + random.nextInt(bc -fc);110 int b = fc + random.nextInt(bc -fc);111 return newColor(r, g, b);112 }