验证码生成

本文涉及的产品
简介:

1、验证码引用与生成

 
  1. 验证码:<html:text property="checkcode"></html:text> 
  2.     <img src="image.jsp"><br> 

image.jsp:

 
  1. <%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %> 
  2. <%! 
  3. Color getRandColor(int fc,int bc){//给定范围获得随机颜色 
  4.         Random random = new Random(); 
  5.         if(fc>255) fc=255
  6.         if(bc>255) bc=255
  7.         int r=fc+random.nextInt(bc-fc); 
  8.         int g=fc+random.nextInt(bc-fc); 
  9.         int b=fc+random.nextInt(bc-fc); 
  10.         return new Color(r,g,b); 
  11.         } 
  12. %> 
  13. <% 
  14. //设置页面不缓存 
  15. response.setHeader("Pragma","No-cache"); 
  16. response.setHeader("Cache-Control","no-cache"); 
  17. response.setDateHeader("Expires"0); 
  18.  
  19. // 在内存中创建图象 
  20. int width=60, height=20
  21. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
  22.  
  23. // 获取图形上下文 
  24. Graphics g = image.getGraphics(); 
  25.  
  26. //生成随机类 
  27. Random random = new Random(); 
  28.  
  29. // 设定背景色 
  30. g.setColor(getRandColor(200,250)); 
  31. g.fillRect(00, width, height); 
  32.  
  33. //设定字体 
  34. g.setFont(new Font("Times New Roman",Font.PLAIN,18)); 
  35.  
  36. //画边框 
  37. //g.setColor(new Color()); 
  38. //g.drawRect(0,0,width-1,height-1); 
  39.  
  40.  
  41. // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 
  42. g.setColor(getRandColor(160,200)); 
  43. for (int i=0;i<155;i++) 
  44.     int x = random.nextInt(width); 
  45.     int y = random.nextInt(height); 
  46.         int xl = random.nextInt(12); 
  47.         int yl = random.nextInt(12); 
  48.     g.drawLine(x,y,x+xl,y+yl); 
  49.  
  50. // 取随机产生的认证码(4位数字) 
  51. //String rand = request.getParameter("rand"); 
  52. //rand = rand.substring(0,rand.indexOf(".")); 
  53. String sRand=""
  54. for (int i=0;i<4;i++){ 
  55.     String rand=String.valueOf(random.nextInt(10)); 
  56.     sRand+=rand; 
  57.     // 将认证码显示到图象中 
  58.     g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 
  59.     g.drawString(rand,13*i+6,16); 
  60.  
  61. // 将认证码存入SESSION 
  62. session.setAttribute("ccode",sRand); 
  63.  
  64.  
  65. // 图象生效 
  66. g.dispose(); 
  67.  
  68. // 输出图象到页面 
  69. ImageIO.write(image, "JPEG", response.getOutputStream()); 
  70.  
  71.  
  72. %>  

2、判断验证码是否正确:从session中取出验证码

 
  1. // 先判断验证码是否正确 
  2.         String ccode = (String) request.getSession().getAttribute("ccode"); 
  3.         String checkcode = userForm.getCheckcode(); 
  4.         if (!(checkcode.equals(ccode))) { 
  5.             ActionMessages errors = new ActionMessages(); 
  6.             errors.add("checkcode"new ActionMessage("checkcode.error")); 
  7.             super.saveErrors(request, errors); 
  8.             return mapping.getInputForward(); 
  9.         } 

 


本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/682287,如需转载请自行联系原作者

相关实践学习
基于函数计算一键部署掌上游戏机
本场景介绍如何使用阿里云计算服务命令快速搭建一个掌上游戏机。
建立 Serverless 思维
本课程包括: Serverless 应用引擎的概念, 为开发者带来的实际价值, 以及让您了解常见的 Serverless 架构模式
相关文章
|
30天前
|
C++
|
1月前
生成验证码
生成验证码
16 0
|
10月前
|
缓存 前端开发 NoSQL
南南的文章-验证码还能这样生成?
南南的文章-验证码还能这样生成?
62 0
南南的文章-验证码还能这样生成?
|
缓存 JavaScript 安全
|
机器学习/深度学习 人工智能 前端开发
关于验证码,你不知道的一些问题!
关于验证码,大家也许会有很多疑问,下面我总结了一些常见问题。
关于验证码,你不知道的一些问题!
|
前端开发 C# 数据安全/隐私保护
C#验证码
验证码通常是为了区分用户是人还是计算机,也可以防止破解密码、刷票等恶意行为,而客户端上多数会用在关键操作上,比如购买、登录、注册等场景。现在验证码的种类样式也特别多,今天教大家如何用C#做出滑动拼图验证码吧~
C#验证码
|
C# 图形学 索引

热门文章

最新文章