在贴代码之前首先简述一下验证验证码原理:随机获取验证码的值,把这个值存到session中,其作用可想而知就是要拿来跟前台数据作比较,通过Graphics将值进行模糊处理之后传到前台页面展示。
1 package com.skss.util; 2 3 4 import java.awt.Color; 5 import java.awt.Font; 6 import java.awt.Graphics; 7 import java.awt.image.BufferedImage; 8 import java.io.IOException; 9 import java.util.Random; 10 11 import javax.imageio.ImageIO; 12 import javax.servlet.ServletException; 13 import javax.servlet.ServletOutputStream; 14 import javax.servlet.http.HttpServlet; 15 import javax.servlet.http.HttpServletRequest; 16 import javax.servlet.http.HttpServletResponse; 17 import javax.servlet.http.HttpSession; 18 19 @SuppressWarnings("serial") 20 public class ValidateCodeServlet extends HttpServlet { 21 char[] codeSequence={‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, 22 ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, 23 ‘X‘, ‘Y‘, ‘Z‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘ }; 24 public ValidateCodeServlet() { 25 super(); 26 } 27 28 public void destroy() { 29 super.destroy(); 30 } 31 32 public void doGet(HttpServletRequest request, HttpServletResponse response) 33 throws ServletException, IOException { 34 this.doPost(request, response); 35 } 36 37 public void doPost(HttpServletRequest request, HttpServletResponse response) 38 throws ServletException, IOException { 39 String params = request.getParameter("parame"); 40 response.setContentType("image/jpeg"); 41 response.setHeader("Pragma","No-cache"); 42 response.setHeader("Cache-Control","no-cache"); 43 response.setDateHeader("Expires", 0); 44 HttpSession session=request.getSession(); 45 // 在内存中创建图象 46 int width=60, height=20; 47 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 48 // 获取图形上下文 49 Graphics g = image.getGraphics(); 50 //生成随机类 51 Random random = new Random(); 52 // 设定背景色 53 g.setColor(getRandColor(200,250)); 54 g.fillRect(0, 0, width, height); 55 //设定字体 56 g.setFont(new Font("Times New Roman",Font.PLAIN,18)); 57 //画边框 58 //g.setColor(new Color()); 59 //g.drawRect(0,0,width-1,height-1); 60 // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 61 g.setColor(getRandColor(160,200)); 62 for (int i=0;i<155;i++) { 63 int x = random.nextInt(width); 64 int y = random.nextInt(height); 65 int xl = random.nextInt(12); 66 int yl = random.nextInt(12); 67 g.drawLine(x,y,x+xl,y+yl); 68 } 69 // 取随机产生的认证码(4位数字) 70 String sRand=""; 71 for (int i=0;i<4;i++){ 72 String rand=String.valueOf(random.nextInt(10)); 73 74 if(Integer.parseInt(rand)%2==0){ 75 rand=codeSequence[random.nextInt(36)]+""; 76 } 77 78 sRand+=rand; 79 // 将认证码显示到图象中 80 g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 81 g.drawString(rand,13*i+6,16); 82 } 83 // 将认证码存入SESSION 84 session.setAttribute(params,sRand); 85 // 图象生效 86 g.dispose(); 87 ServletOutputStream responseOutputStream =response.getOutputStream(); 88 // 输出图象到页面 89 ImageIO.write(image, "JPEG", responseOutputStream); 90 //以下关闭输入流! 91 responseOutputStream.flush(); 92 responseOutputStream.close(); 93 } 94 Color getRandColor(int fc,int bc){//给定范围获得随机颜色 95 Random random = new Random(); 96 if(fc>255) fc=255; 97 if(bc>255) bc=255; 98 int r=fc+random.nextInt(bc-fc); 99 int g=fc+random.nextInt(bc-fc); 100 int b=fc+random.nextInt(bc-fc); 101 return new Color(r,g,b); 102 } 103 }
上面的代码注释想必已经描述的很清楚了,这里就不细说了。注意String params = request.getParameter("parame");session.setAttribute(params,sRand); 这是关键
1 <servlet> 2 <description>vCode</description> 3 <display-name>vCode</display-name> 4 <servlet-name>ValidateCodeServlet</servlet-name> 5 <servlet-class> 6 com.skss.util.ValidateCodeServlet 7 </servlet-class> 8 </servlet> 9 <servlet-mapping> 10 <servlet-name>ValidateCodeServlet</servlet-name> 11 <url-pattern>/vcode</url-pattern> 12 </servlet-mapping>
web.xml代码,下面是vcode.jsp页面,后面的参数为了让这个验证码通用,同时保正每个页面的验证码不会因为其他引用的验证码刷新而改变
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" 5 + request.getServerName() + ":" + request.getServerPort() 6 + path + "/"; 7 String type = request.getParameter("type"); 8 %> 9 <script> 10 function refresh(){ 11 document.location.href = document.location.href; 12 } 13 </script> 14 <img src="${path}/vcode?parame=<%= type%>" onclick="refresh()" style="cursor: pointer;" title="看不清!点击换一个!"/>
下面是引用这个jsp的iframe
1 <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes" src="${path}/app/admin/login/vcode.jsp?type=login_vcode" style="width: 60px;height: 20px;padding: 0,0,0,0;" scrolling="no" ></iframe>
if (!user.getVcode().equalsIgnoreCase( (String) this.request.getSession().getAttribute("login_vcode"))) { request.setAttribute("msg", "验证码错误!"); return "loginx"; }
时间: 2024-10-23 04:37:13