实现原理(图)
验证码的实现原理和防止重复提交的token验证机制差不多。
(1)客户端发送请求到服务器端,服务器送回登录页面。
(2)因为登录页面上有一个验证码图片,所以在显示验证码的时候,客户端又会发送请求到专门产生图片的servlet,此时这个servlet先产生一个验证码放入session作用域,然后生成验证码图片,将验证码图片传给客户端。
(3)当登录页面表单提交后,将用户输入的(重复提交里是将服务器生成的token放在hidden组件里,而这里是手动输入而已)验证码和session作用域中的验证码进行对比
代码中一些知识细节
1、Math.random() 返回的是个double数,范围[0.0,1.0)
2、Math.ceil() 返回最小的(最接近负无穷)的双值,它大于或等于参数,等于一个数学整数。
3、‘A’ 的ASCII码值为65,是最小的,字母26个, ‘A’+26。
4、c = (char) ((char) ((int) (Math.random() * 26)) + ‘A’); 这是A-Z的代码,没有小写的情况
5、它 是用在 窗口的 pait(Graphics g)里面的,用处就是来画出窗口的组建或图片
代码实现
package com.neuedu;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ValidateCodeServlet
*/
@WebServlet("/ValidateCodeServlet")
public class ValidateCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Random r = new Random();
/**
* @see HttpServlet#HttpServlet()
*/
public ValidateCodeServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//随机数
Random random = new Random();
//验证码的位数
int size = 4;
//保存生成的验证码
String vCode="";
char c;
//产生验证码
for (int i = 0; i < size; i++) {
//产生一个26以内的随机整数
int number = random.nextInt(26);
//如果生成的是偶数,则随机生成一个数字
if(number %2 ==0){
c = (char) ('0' + (char) ((int) (Math.random() * 10)));
//奇数,则随机生成一个字母(只有大写)
}else{
c = (char)((char) (int) (Math.random()*26)+'A');
}
vCode = vCode + c;
}
//保存生成的4为验证码
request.getSession().setAttribute("vCode",vCode);
/*
* 验证码图片的生成
*/
//定义图片的宽度和高度
int width = (int) Math.ceil(size * 20);
int height = 50;
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
//获取图片的上下文
Graphics gr = image.getGraphics();
//设定图片背景颜色
gr.setColor(Color.WHITE);
gr.fillRect(0, 0, width, height);
//设定图片边框
gr.setColor(Color.GRAY);
gr.drawRect(0, 0, width - 1, height - 1);
//画十干扰线
for (int i = 0; i < 5; i++) {
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
int x2 = random.nextInt(width);
int y2 = random.nextInt(height);
//干扰线的颜色随机
gr.setColor(randomColor());
gr.drawLine(x1, y1, x2, y2);
}
//设置字体和颜色,画验证码
gr.setColor(randomColor());
gr.setFont(randomFont());
gr.drawString(vCode, 10, 22);
//图像生效
gr.dispose();
//输出到页面,以流的形式,图片类型JPES
ImageIO.write(image, "JPEG", response.getOutputStream());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
/*
* 生成随机的颜色
*/
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 String[] fontNames = {"宋体","华文楷体","黑体","微软雅黑","楷体_GB2312"};
/*
* 生成随机的字体
*/
private Font randomFont(){
int index = r.nextInt(fontNames.length);
String fontName = fontNames[index];//生成随机的字体名称
int style = r.nextInt(5);
int size = r.nextInt(3)+20; //生成随机字号,20~24
return new Font(fontName,style , size);
}
}
JSP实现:
<title>登录界面到验证</title>
<script src="js/jquery.min.js"></script>
<script>
$(function () {
$("#refresh").click(function(){
var date = new Date();
$("#img").attr('src',"ValidateCodeServlet?"+date);
})
});
</script>
</head>
<body>
<div class="login">
<div class="message">欢迎登陆</div>
<hr class="hr15">
<div id="darkbannerwrap"></div>
<form action="" method="post">
<input name = "username" placeholder="用户名" type="text">
<hr class="hr15">
<input name="password" placeholder="密码" type="password">
<hr class="hr15">
<input type="submit">
<hr class="hr15">
<input name="check_code" placeholde="验证码" type="text" style="width:30%">
<img id="img" alt="验证码" src="ValidateCodeServlet" />
<a id="refresh" href="javascript:void(0)">换一张</a>
<hr class="hr15">
</form>
</div>
</body>
</html>