JAVA生成验证码代码
Posted 来陪朕侍寝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA生成验证码代码相关的知识,希望对你有一定的参考价值。
生成base64格式图片验证码
1 /** 2 * 生成验证码 3 * 改造生成验证码的方式,将图片base64形式传到前台,而不是直接传验证码到前台 4 * @return 5 * @throws IOException 6 */ 7 public void imageCode() throws IOException { 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=new HashMap(); 15 16 // 在内存中创建图象 17 int width = 65, height = 38; 18 BufferedImage image = new BufferedImage(width, height, 19 BufferedImage.TYPE_INT_RGB); 20 // 获取图形上下文 21 Graphics g = image.getGraphics(); 22 // 生成随机类 23 Random random = new Random(); 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 = new StringBuffer(); 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<(random.nextInt(5)+5);i++){ 42 g.setColor(new Color(random.nextInt(255)+1,random.nextInt(255)+1,random.nextInt(255)+1)); 43 g.drawLine(random.nextInt(70),random.nextInt(40),random.nextInt(70),random.nextInt(40)); 44 g.drawLine(random.nextInt(70),random.nextInt(40),random.nextInt(70),random.nextInt(40)); 45 } 46 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 = new ByteArrayOutputStream(); 70 ImageIO.write(image, "jpg", outputStream); 71 BASE64Encoder encoder = new BASE64Encoder(); 72 String base64Img = encoder.encode(outputStream.toByteArray()); 73 base64Img="data:image/jpg;base64, "+base64Img.replaceAll(" ", "").replaceAll(" ", "");//删除 ; 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 * @param fc 99 * @param bc 100 * @return 101 */ 102 Color getRandColor(int fc, int bc) { 103 Random random = new Random(); 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 new Color(r, g, b); 112 }
以上是关于JAVA生成验证码代码的主要内容,如果未能解决你的问题,请参考以下文章