用户登录注册之验证码技术实现_第1页
用户登录注册之验证码技术实现_第2页
用户登录注册之验证码技术实现_第3页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、用户登录注册之验证码技术实现对于一些恶意强暴破解密码的行为(即通过硬性尝试用 户名密码进行破解) ,可以采用验证码对其进行抵御,对于 一些程序可以识别验证码,则需要对验证码形式进行多样化 设计。 注!拿到的页面是只有图片,需要用另一个页面引用图 片页面 示例: <img src = "xxx.jsp"> 用户登录时设置验 证码代码实现:页面 <script type="text/javascript"> function _change() var imgEle = document.getElem

2、entById("img"); imgEle.src =+ new Date().getTime();</script><BODY><TR><TD style="HEIGHT: 28px"> 验证码:</TD><TD style="HEIGHT: 28px"><input type="text" name="verifyCod

3、e" size="1"/><br/><a href="javascript:_change()"> 一张 </a></TD><a href="#"></a><TD style="HEIGHT: 28px"><SPAN id=RequiredFieldValidator4 style=&

4、quot;FONT-WEIGHT: bold;VISIBILITY: hidden; COLOR: white"> 请输入验证码 </SPAN></TD></TR><TR></FORM></BODY>2.action 操作/获得验证码public void getVerify() throws IOException HttpServletRequest request=ServletActionCont

5、ext.getRequest();HttpServletResponse response=ServletActionContext.getResponse();VerifyCode vc=new VerifyCode();BufferedImage image=vc.getImage();/ 获得图片request.getSession().setAttribute("session_vcode",vc.g etText();/ 将验证码内容放在域对象里面 VerifyCode.output(image, response.getOutputStream();public

6、 String login()HttpServletRequest request=ServletActionContext.getRequest();String sessionCode = (String)request.getSession().getAttribute("session_vcode");String paramCode =request.getParameter("verifyCode");if(!paramCode.equalsIgnoreCase(sessionCode) request.setAttribute("

7、msg", " 验证码错误! "); return "login"User userExit=userService.login(user);if(userExit!=null)if(!userExit.isState()request.setAttribute("msg", " 您尚未激活,请到邮箱"+userExit.getEmail()+"激活!");return "loginerror" request=ServletActionContext.getReq

8、uest(); request.getSession().setAttribute("user",userExit);return "loginsuccess"elsereturn "login"3. 验证码代码实现 public class VerifyCode private int w = 70;private int h = 35;private Random r = new Random();/ "宋体", "华文楷体 ", "黑体", "华文新魏 &qu

9、ot;, "华文隶书微软雅黑","楷体_GB2312"黑体private String fontNames = " 宋体 ", "华文楷体 ", ","微软雅黑","楷体_GB2312"/ 可选字符private String codes ="23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ"/ 背景色private Color bgColor= new Color(255, 255,

10、 255);/ 验证码上的文本private String text ;/ 生成随机的颜色private Color randomColor () int red = r.nextInt(150);int green = r.nextInt(150);int blue = r.nextInt(150);return new Color(red, green, blue);/ 生成随机的字体private Font randomFont () int index = r.nextInt(fontNames.length);String fontName = fontNamesindex;/ 生成

11、随机的字 体名称int style = r.nextInt(4);/ 生成随机的样式 , 0( 无样式 ), 1(粗体), 2(斜体), 3(粗体+斜体)int size = r.nextInt(5) + 24; / 生成随机字号 , 24 28 return new Font(fontName, style, size);/ 画干扰线private void drawLine (BufferedImage image) int num = 3;/ 一共画 3 条Graphics2D g2 = (Graphics2D)image.getGraphics();for(int i = 0; i &

12、amp;lt; num; i+) / 生成两个点的坐标, 即 4 个值int x1 = r.nextInt(w);int y1 = r.nextInt(h);int x2 = r.nextInt(w);int y2 = r.nextInt(h);g2.setStroke(new BasicStroke(1.5F);g2.setColor(Color.BLUE); / 干扰线是蓝色 g2.drawLine(x1, y1, x2, y2);/ 画线/ 随机生成一个字符private char randomChar () int index = r.nextInt(codes.length();re

13、turn codes.charAt(index);/ 创建 BufferedImageprivate BufferedImage createImage () BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);Graphics2D g2 = (Graphics2D)image.getGraphics(); g2.setColor(this.bgColor);g2.fillRect(0, 0, w, h);return image;/ 调用这个方法得到验证码public BufferedImage

14、getImage () BufferedImage image = createImage();/ 创建图片缓 冲区Graphics2D g2 =(Graphics2D)image.getGraphics();/ 得到绘制环境StringBuilder sb = new StringBuilder();/用来装载生成的验证码文本/ 向图片中画 4 个字符for(int i = 0; i < 4; i+) / 循环四次,每次生成一个 字符String s = randomChar() + ""/ 随机生成一个字母 sb.append(s); / 把字母添加到 sb 中float x = i * 1.0F * w / 4; / 设置当前字符的 x 轴坐标 g2.setFont(randomFont(); / 设置随机字体 g2.setColor(randomColor(); / 设置随机颜色 g2.drawString(s, x, h-5); / 画图this.text = sb.toString(); / 把生成的字符串赋给了this.text

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论