javaWeb中的验证码
在做网站中,经常用到验证码验证什么的。如登陆验证...随着技术的升级,验证码技术也升级了。验证码的出现时为了防止‘暴力破解’账号。下面我就讲我最近所学的一个普通写验证码的代码整理如下:
第一步:servlet中的代码
1 package com.zuxia.servlet; 2 3 import java.awt.BasicStroke; 4 import java.awt.Color; 5 import java.awt.Font; 6 import java.awt.Graphics; 7 import java.awt.Graphics2D; 8 import java.awt.GraphicsEnvironment; 9 import java.awt.geom.AffineTransform; 10 import java.awt.image.BufferedImage; 11 import java.io.IOException; 12 import java.util.Random; 13 14 import javax.imageio.ImageIO; 15 import javax.servlet.ServletException; 16 import javax.servlet.http.HttpServlet; 17 import javax.servlet.http.HttpServletRequest; 18 import javax.servlet.http.HttpServletResponse; 19 import javax.servlet.http.HttpSession; 20 21 public class DrawImageCode extends HttpServlet { 22 23 private Color getRandColor(int s, int e) { 24 Random random = new Random(); 25 if (s > 255) 26 s = 255; 27 if (e > 255) 28 e = 255; 29 int r = random.nextInt(e - s) + s; // 随机生成RGB中的R值 30 int g = random.nextInt(e - s) + s; // 随机生成RGB中的G值 31 int b = random.nextInt(e - s) + s; // 随机生成RGB中的B值 32 return new Color(r, g, b); 33 } 34 35 36 public void doGet(HttpServletRequest request, HttpServletResponse response) 37 throws ServletException, IOException { 38 39 this.doPost(request, response); 40 } 41 42 public void doPost(HttpServletRequest request, HttpServletResponse response) 43 throws ServletException, IOException { 44 45 response.setHeader("Pragma", "No-cache"); 46 response.setHeader("Cache-Control", "No-cache"); 47 response.setDateHeader("Expires", 0); 48 //指定生成的响应图片 49 response.setContentType("image/jpeg"); 50 int width = 150; //指定生成验证码图片的宽度 51 int heigh = 60; //指定生成验证码图片的高度 52 //获得图像数据缓冲对象 53 BufferedImage bi = new BufferedImage(width,heigh,BufferedImage.TYPE_INT_RGB); 54 //获得图形上下文的抽象基类 55 Graphics g = bi.getGraphics(); 56 Random r = new Random(); 57 Font f = new Font("黑体",Font.PLAIN,60); //定义字体样式 58 g.setColor(getRandColor(200, 250)); //设置背景颜色 59 g.fillRect(0, 0, width, heigh); //绘制背景 60 g.setFont(f); //设置字体 61 g.setColor(getRandColor(180, 200)); //设置前景颜色 62 63 64 Graphics2D g2d = (Graphics2D) g; 65 //画一条折线 66 //BasicStroke bs=new BasicStroke(5f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL); //创建一个供画笔选择线条粗细的对象 67 //g2d.setStroke(bs); //改变线条的粗细 68 g2d.setColor(Color.DARK_GRAY); //设置当前颜色为预定义颜色中的深灰色 69 int[] xPoints=new int[8]; 70 int[] yPoints=new int[8]; 71 for(int j=0;j<8;j++){ 72 xPoints[j]=r.nextInt(width - 1); 73 yPoints[j]=r.nextInt(heigh - 1); 74 } 75 g2d.drawPolyline(xPoints, yPoints,8); 76 77 String sRand = ""; 78 String ctmp = ""; 79 int itmp = 0; 80 for (int i = 0; i < 6; i++) { 81 switch (r.nextInt(2)) { 82 case 1: 83 itmp = r.nextInt(26) + 65; // 生成A~Z的字母 84 ctmp = String.valueOf((char) itmp); 85 break; 86 default: 87 itmp = r.nextInt(10) + 48; // 生成0~9的数字 88 ctmp = String.valueOf((char) itmp); 89 break; 90 } 91 sRand += ctmp; 92 //设置每个字符的随机颜色 93 Color color = new Color(20 + r.nextInt(210), 20 + r.nextInt(210), 20 + r.nextInt(210)); 94 g2d.setColor(color); 95 //设置每个字符的随机字体 96 g2d.setFont(this.getRandomFont()); 97 //设置每个字符的随机旋转 98 AffineTransform at = new AffineTransform(); 99 int number = r.nextInt(3)-1; 100 double role = number*r.nextDouble()*(Math.PI/4); 101 at.setToRotation(role,25*i+5,55-r.nextInt(20)); //role:旋转角度,后面两个参数是设置围绕坐标点旋转 102 g2d.setTransform(at); 103 104 g2d.drawString(ctmp, 25*i+5,55-r.nextInt(20)); 105 } 106 HttpSession session = request.getSession(true); 107 session.setMaxInactiveInterval(5*60);//设置session对象5分钟失效 108 session.setAttribute("code", sRand.toString()); 109 110 g2d.dispose(); 111 112 System.out.println(sRand); 113 ImageIO.write(bi, "JPEG", response.getOutputStream()); 114 115 116 } 117 118 119 public static Font getRandomFont() { 120 //String[] fonts = {"Georgia", "Verdana", "Arial", "Tahoma", "Time News Roman", "Courier New", "Arial Black", "Quantzite"}; 121 String[] fonts = {"a_TrianglerCmUp","Astarisborn","WishfulWaves"}; 122 Random r = new Random(); 123 int fontIndex = r.nextInt(fonts.length); 124 return new Font(fonts[fontIndex], r.nextInt(3),50-r.nextInt(20)); 125 } 126 }
第二步:jsp页面引用这servlet,用以显示效果
1 <html> 2 <head> 3 <tiltle>我是验证码页面哦</title> 4 5 </head> 6 7 <body> 8 <div><img src="DrawImageCode " alt="验证码"/></div> 9 </body> 10 </html>
提示:DrawImageCode是已建好的servlet的路径名哦......