1 /**
2 * 生成验证码3 * 改造生成验证码的方式,将图片base64形式传到前台,而不是直接传验证码到前台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 //生成随机类
23 Random random = newRandom();24 //设定背景色
25 g.setColor(getRandColor(230, 255));26 g.fillRect(0, 0, 100, 40);27 //设定字体
28 g.setFont(new Font(“Arial”, Font.CENTER_BASELINE | Font.ITALIC, 20));29 //产生0条干扰线,
30 g.drawLine(0, 0, 0, 0);31
32 //存放验证码
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 //将认证码显示到图象中
38 g.setColor(getRandColor(100, 150));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
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 //以下关闭输入流!
84 responseOutputStream.flush();85 responseOutputStream.close();86 //获得页面key值
87 return;88 } catch(IOException e) {89 logger.error(“生产验证码出错”,e);90 throw new SystemException(“生产验证码出错”,e);91 }92 }93
94
95 /**
96 * 给定范围获得随机颜色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 }