实验1_验证码的Java实现.doc_第1页
实验1_验证码的Java实现.doc_第2页
实验1_验证码的Java实现.doc_第3页
实验1_验证码的Java实现.doc_第4页
实验1_验证码的Java实现.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

南昌航空大学实验报告2015年 月 日课程名称: 信息安全概论 实验名称: 验证码的Java实现 学 号: 姓 名: 指导老师评定: 签 名: 一、实验目的通过实验了解验证码的原理,掌握Java语言验证码的实现。二、实验原理1.验证码的定义验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自动区分计算机和人类的图灵测试)的缩写,是一种区分用户是计算机还是人的公共全自动程序。这个问题可以由计算机生成并评判,但是必须只有人类才能解答。由于计算机无法解答CAPTCHA的问题,所以回答出问题的用户就可以被认为是人类。2.验证码的作用:可以防止恶意破解密码、刷票、论坛灌水,有效防止黑客对注册用户用特定程序暴力破解方式进行不断地登录尝试,实际上用验证码是现在很多网站通行的方式。利用比较简易的方式实现这个功能,虽然登录麻烦一点,但是对用户的密码安全来说这个功能还是很有必要,也很重要。3.验证码的使用大多数网站的验证码都是需要点击一下填写框,然后会自动弹出验证码图片。由于验证码是随机产生的,有很大几率会出现无法清楚识别的验证码图片,所以需要注意的是,一般网站都会有相应的提示,如“看不清,换一张”等,如果没有提示,则直接点击当前的验证码图片,可以完成验证码的更换。(1)登录时使用:防止暴力破解密码(2)注册时使用:防止自动批量注册(3)发帖时使用:防止自动灌水、发广告三、实验环境开发工具:JDK、Eclipse参考资料:JDK API文档java.awt包java.awt.event包javax.swing包四、实验内容和任务使用Java语言编程,实现登录时生成验证码的功能。五、实验要求(1)登录时,系统随机生成验证码,并显示在图片中;(2)用户输入用户名、密码和验证码后,系统先判断验证码是否正确,如果不正确,则重新随机生成一个新的验证码,等待用户再次输入;(3)如果输入的验证码正确,再判断用户名和密码是否正确,判断是否登录成功。六、程序代码(1)创建RandomImageGenerator.java类,实现验证码图片的生成。package com.tenghu.code;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Random;import javax.imageio.ImageIO;/* * 验证码生成类 * author xiaohu * */public class RandomImageGenerator /创建Random对象 static Random random=new Random(); /随机生成包含验证码字符串 public static String random(int num) String str=0,1,2,3,4,5,6,7,8,9, a,b,c,d,e,f,g,h,i,j, k,l,m,n,p,q,r,s,t; int number=str.length; /接收随机字符 String text = ; /随机产生4个字符的字符串 for(int i=0;inum;i+) text+=strrandom.nextInt(number); return text; /* * 随机产生定义的颜色 * * return */ private static Color getRandColor() Random random = new Random(); Color color = new Color10; color0 = new Color(32, 158, 25); color1 = new Color(218, 42, 19); color2 = new Color(31, 75, 208); color3 = new Color(0, 102, 182); color4 = new Color(171, 0, 85); return colorrandom.nextInt(5); /* * 产生随机字体 * * return */ private static Font getFont() Random random = new Random(); Font font = new Font5; font0 = new Font(Ravie, Font.BOLD, 30); font1 = new Font(Antique Olive Compact, Font.BOLD, 30); font2 = new Font(Forte, Font.BOLD, 30); font3 = new Font(Wide Latin, Font.BOLD, 30); font4 = new Font(Gill Sans Ultra Bold, Font.BOLD, 30); return fontrandom.nextInt(5); /* * 生成图片 * throws IOException */ public static void render(String randomStr,OutputStream out,int width,int height) throws IOException /在内存中创建图像 BufferedImage bi=new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED); /获取图形上下文 Graphics2D g=(Graphics2D) bi.getGraphics(); /话边框 g.setColor(Color.white); g.fillRect(0, 0, width, height); g.setFont(getFont(); g.setColor(Color.BLACK); /画认证码,每个认证码在不同的水平位置 String str1=new StringrandomStr.length(); for(int i=0;istr1.length;i+) str1i=randomStr.substring(i,i+1); int w=0; int x=(i+1)%3; /随机生成验证码字符水平偏移量 if(x=random.nextInt(7) w=30-random.nextInt(7); else w=30+random.nextInt(7); /随机生成颜色 g.setColor(getRandColor(); g.drawString(str1i, 20*i+10, w); /随机产生干扰点,并用不同的颜色表示,事图像的认证码不易被其他程序探测到 for(int i=0;i100;i+) int x=random.nextInt(width); int y=random.nextInt(height); Color color=new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255); /随机画各种颜色的线 g.setColor(color); g.drawOval(x, y, 0, 0); /画干扰线 for(int i=0;i15;i+) int x=random.nextInt(width); int y=random.nextInt(height); int x1=random.nextInt(width); int y1=random.nextInt(height); Color color=new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255); /随机画各种颜色线 g.setColor(color); g.drawLine(x, y, x1, y1); /图像生效 g.dispose(); /输出页面 ImageIO.write(bi, jpg, out); public static void main(String args) throws FileNotFoundException, IOException /获取随机字符串 String randomStr=random(5); System.out.println(randomStr); /生成图片 render(randomStr, new FileOutputStream(D:test.jpg),130,40); (2).创建RandomImageServlet.java,将生成的验证码输出到页面ackage com.tenghu.code.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.tenghu.code.RandomImageGenerator;public class RandomImageServlet extends HttpServlet /图片宽度 int width=0; /图片高度 int height=0; /图片上随机字符个数 int randomStrNum=0; public void destroy() public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doPost(request, response); public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException request.setCharacterEncoding(UTF-8); /获取HttpSession对象 HttpSession session=request.getSession(); /获取随机字符串 String randomStr=RandomImageGenerator.random(randomStrNum); if(null!=session) /设置参数 session.setAttribute(randomStr, randomStr); /设置响应类型,输出图片客户端不缓存 response.setDateHeader(Expires, 1L); response.setHeader(Cache-Control, no-cache, no-store, max-age=0); response.addHeader(Pragma, no-cache); response.setContentType(image/jpeg); /输出到页面 RandomImageGenerator.render(randomStr, response.getOutputStream(), width, height); public void init() throws ServletException /获取宽度 width=Integer.parseInt(this.getInitParameter(width); /获取高度 height=Integer.parseInt(this.getInitParameter(height); /获取个数 randomStrNum=Integer.parseInt(this.getInitParameter(num); (3).创建LoginAction.java类,控制登录package com.tenghu.code.action;import java.io.ByteArrayInputStream;import java.io.InputStream;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport /用户名 private String userName; /密码 private String password; /验证码 private String code; private InputStream inputStream; public InputStream getResult() return inputStream; /成功 public String success() throws Exception return SUCCESS; /测试登录 public String testLogin() throws Exception /获取图片的验证码 String randomStr=(String) ActionContext.getContext().getSession().get(rando

温馨提示

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

评论

0/150

提交评论