Java 登陆验证码

来源:互联网 发布:鬼吹灯里的怪物 知乎 编辑:程序博客网 时间:2024/06/01 21:47

生成验证的类:RandomCode

 

import Java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;

import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@SuppressWarnings("serial")
public class RandomCode extends HttpServlet {

 /**
  * The doGet method of the servlet. <br>
  * 
  * This method is called when a form. has its tag value method equals to
  * get.
  * 
  * @param request
  *            the request send by the client to the server
  * @param response
  *            the response send by the server to the client
  * @throws ServletException
  *             if an error occurred
  * @throws IOException
  *             if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  this.doPost(request, response);
 }

 /**
  * The doPost method of the servlet. <br>
  * 
  * This method is called when a form. has its tag value method equals to
  * post.
  * 
  * @param request
  *            the request send by the client to the server
  * @param response
  *            the response send by the server to the client
  * @throws ServletException
  *             if an error occurred
  * @throws IOException
  *             if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  Random random = new Random();

  int width = 60, height = 20;
  // 创建BufferedImage对象,设置图片的长度宽度和色彩。
  BufferedImage image = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_RGB);
  OutputStream os = response.getOutputStream();
  // 取得Graphics对象,用来绘制图片
  Graphics g = image.getGraphics();
  // 绘制图片背景和文字,释放Graphics对象所占用的资源。
  g.setColor(getRandColor(200, 250));
  // 设置内容生成的位置
  g.fillRect(0, 0, width, height);
  // 设置内容的字体和大小
  g.setFont(new Font("Times New Roman", Font.PLAIN, 20));

  // 设置内容的颜色:主要为生成图片背景的线条
  g.setColor(getRandColor(160, 200));
  // 画边框。
  g.setColor(Color.BLACK);
  g.drawRect(0, 0, width - 1, height - 1);
  // 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。
  g.setColor(Color.BLACK);
  // 图片背景上随机生成155条线条,避免通过图片识别破解验证码
  for (int i = 0; i < 30; i++) {
   int x = random.nextInt(width);
   int y = random.nextInt(height);
   int xl = random.nextInt(12);
   int yl = random.nextInt(12);
   g.drawLine(x, y, x + xl, y + yl);
  }

  // 生成四位的随机数,生成一个数,写一个
  String[] s = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
    "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
    "X", "Y", "Z" };
  String content = "";
  for (int i = 0; i < 4; i++) {
   String rand = "";
   if (random.nextBoolean()) {
    rand = String.valueOf(random.nextInt(10));
   } else {
    int index = random.nextInt(25);
    rand = s[index];
   }
   content += rand;
   g.setColor(new Color(20 + random.nextInt(10), 20 + random
     .nextInt(110), 20 + random.nextInt(110)));
   g.drawString(rand, 13 * i + 6, 16);
  }

  // 释放此图形的上下文以及它使用的所有系统资源,类似于关闭流
  g.dispose();

  // 将生成的验证码值(即运算结果的值)放到session中,以便于后台做验证。
  HttpSession session = request.getSession();
  session.setAttribute("result", content);

  // 通过ImageIO对象的write静态方法将图片输出。
  ImageIO.write(image, "JPEG", os);
  os.close();
 }

 /**
  * 生成随机颜色
  * 
  * @param fc
  * @param bc
  * @return
  */
 public Color getRandColor(int fc, int bc) {
  Random random = new Random();
  if (fc > 255)
   fc = 255;
  if (bc > 255)
   bc = 255;
  int r = fc + random.nextInt(bc - fc);
  int g = fc + random.nextInt(bc - fc);
  int b = fc + random.nextInt(bc - fc);
  return new Color(r, g, b);
 }
}

 

 

 

 

web.xml配置:

 

 <!-- 验证码 -->
 <servlet>
    <description>生成验证码的Servlet</description>
    <display-name>RandomCode</display-name>
    <servlet-name>RandomCode</servlet-name>
    <servlet-class>com.gxh.tools.RandomCode</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>RandomCode</servlet-name>
    <url-pattern>*.RandomCode</url-pattern>
</servlet-mapping>

 

 

 

 

login.jsp页面:

 

 <script type="text/JavaScript">
   function changeImg()
   {    document.getElementById("validatecodeimg").src=Math.round(Math.random()*10000)+".RandomCode ";
   }
</script>
  <body>
  <div align="center">
  
   <img id="validatecodeimg" alt="看不清?点击刷新" onclick="javascript:changeImg()" src="first.RandomCode " align="top" />  
<a href="javascript:changeImg()">看不清?点击刷新</a>

</div>
  </body>
</html>

0 0
原创粉丝点击