Java⽣成验证码原理(jsp)
验证码的作⽤:
验证码是Completely Automated Public Turing test to tell Computers and Humans Apart(全⾃动区分计算机和⼈类的图灵测试)的缩写,是⼀种区分⽤户是计算机还是⼈的公共全⾃动程序,可以防⽌:恶意破解密码、刷票、论坛灌⽔、有效防⽌某个⿊客对某⼀特定注册⽤户,⽤特定程序暴⼒破解⽅式进⾏不断的登录尝试。实际上验证码是现在很多⽹站通⾏的⽅式,我们利⽤⽐较简易的⽅式实现了这个功能。
第⼀代:标准验证码
这⼀代验证码是即是我们常见的图形验证码、语⾳验证码,基于机器难以处理复杂的计算机视觉及语⾳识别问题,⽽⼈类却可以轻松的识别来区分⼈类及机器。这⼀代验证码初步利⽤了⼈类知识容易解答,⽽计算机难以解答的机制进⾏⼈机判断。下⾯这种验证码就是第⼀代
Java 中⽣成验证码,⼀个.java就可以了
ValidateCodeServlet.java
package com.hpe.servlet;
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;
@WebServlet("/ValidateCodeServlet")
public class ValidateCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public ValidateCodeServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
java生成随机数的方法protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 响应头信息
response.setHeader("Pragma", "No-Cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expries", 0);
// 随机数⽣成类
Random random = new Random();
// 定义验证码的位数
int size = 5;
// 定义变量保存⽣成的验证码
String vCode = "";
char c;
// 产⽣验证码
for (int i = 0; i < size; i++) {
// 产⽣⼀个26以内的随机整数
int number = Int(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;
}
// 保存⽣成的5位验证码
// 验证码图⽚的⽣成
// 定义图⽚的宽度和⾼度
int width = (int) il(size * 20);
int height = 50;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
/
/ 获取图⽚的上下⽂
Graphics gr = Graphics();
// 设定图⽚背景颜⾊
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 = Int(width);
int y1 = Int(height);
int x2 = Int(width);
int y2 = Int(height);
gr.setColor(randomColor());
gr.drawLine(x1, y1, x2, y2);
}
// 设置字体,画验证码
gr.setColor(randomColor());
gr.setFont(randomFont());
gr.drawString(vCode, 10, 22);
// 图像⽣效
gr.dispose();
// 输出到页⾯
ImageIO.write(image, "JPEG", OutputStream());
}
// ⽣成随机的颜⾊
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 Random r = new Random();
// ⽣成随机的字体
private Font randomFont() {
int index = r.nextInt(fontNames.length);
String fontName = fontNames[index];// ⽣成随机的字体名称
int style = r.nextInt(4);
int size = r.nextInt(3) + 24; // ⽣成随机字号, 24 ~ 28
return new Font(fontName, style, size);
}
}
直接运⾏Java⽣成的结果:
在jsp⽹页上显⽰的效果:
jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<title>login</title>
<link href="css/login.css" type="text/css" rel="stylesheet">
<script type="text/javascript">
function $(id){
ElementById(id);
}
function changeCode(){
$("code").src = "ValidateCodeServlet?a="+new Date();
}
</script>
</head>
<body>
<div class="login">
<div class="message">欢迎登录</div>
<hr class="hr15">
<div id="darkbannerwrap"></div>
<form method="post">
<input name="action" value="login" type="hidden"> <input
name="username" placeholder="⽤户名" required="" type="text">
<hr class="hr15">
<input name="password" placeholder="密码" required="" type="password">
<hr class="hr15">
<input name="check_code" placeholder="验证码" required="" type="text"
> <img class="img_code"
src="ValidateCodeServlet" id="code" /> <a class="a_code"
href="javascript:changeCode()">换⼀张</a>
<hr class="hr15">
</form>
</div> </body> </html>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论