java⽣成图⽚验证码(ssm框架)
验证码在实际应⽤场景中⽐较常见,可以有效避免⿊客对系统的暴⼒破解和攻击。
验证码原理:
1. 后台⽣成⼀串⽆序字母或者数字存放到session并响应到前台页⾯
2. 前台页⾯表单填写验证码提交后台与session中存放的验证码⽐对
jquery框架原理前台页⾯:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/";
%>
<!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>验证码页⾯</title>
<script type="text/javascript" src="<%=path %>/skin/jquery.js"></script>
</head>
<body>
<a href="javascript:getYzcode()">
<img id="yzm_img" title="点击刷新验证码" src="user/getCode"/> </a>
</body>
<script type="text/javascript">
function getYzcode() {
$("#yzm_img").prop('src','user/getCode?a='+new Date().getTime());
}
</script>
</html>
后台控制器:
/**
* 获取验证码
* @param req
* @param resp
* @return
*/
@RequestMapping("getCode")
@ResponseBody
public void getCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
/*
1.⽣成验证码
2.把验证码上的⽂本存在session中
3.把验证码图⽚发送给客户端
*/
CodeUtil ivc = new CodeUtil(); //⽤我们的验证码类,⽣成验证码类对象
BufferedImage image = Image(); //获取验证码
ivc.output(image, OutputStream());//将验证码图⽚响应给客户端
}
⽣成验证码⼯具类:
package com.ssm;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class CodeUtil {
private int weight = 100; //验证码图⽚的长和宽
private int height = 40;
private String text; //⽤来保存验证码的⽂本内容
private Random r = new Random(); //获取随机数对象
//private String[] fontNames = {"宋体", "华⽂楷体", "⿊体", "微软雅⿊", "楷体_GB2312"}; //字体数组
/
/字体数组
private String[] fontNames = {"Georgia"};
//验证码数组
private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
/**
* 获取随机的颜⾊
*
* @return
*/
private Color randomColor() {
int r = Int(225); //这⾥为什么是225,因为当r,g,b都为255时,即为⽩⾊,为了好辨认,需要颜⾊深⼀点。 int g = Int(225);
int b = Int(225);
return new Color(r, g, b); //返回⼀个随机颜⾊
}
/**
* 获取随机字体
*
* @return
*/
private Font randomFont() {
int index = r.nextInt(fontNames.length); //获取随机的字体
String fontName = fontNames[index];
int style = r.nextInt(4); //随机获取字体的样式,0是⽆样式,1是加粗,2是斜体,3是加粗加斜体
int size = r.nextInt(10) + 24; //随机获取字体的⼤⼩
return new Font(fontName, style, size); //返回⼀个随机的字体
}
/**
* 获取随机字符
*
* @return
*/
private char randomChar() {
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
/**
* 画⼲扰线,验证码⼲扰线⽤来防⽌计算机解析图⽚
*
* @param image
*/
*/
private void drawLine(BufferedImage image) {
int num = r.nextInt(10); //定义⼲扰线的数量
Graphics2D g = (Graphics2D) Graphics();
for (int i = 0; i < num; i++) {
int x1 = r.nextInt(weight);
int y1 = r.nextInt(height);
int x2 = r.nextInt(weight);
int y2 = r.nextInt(height);
g.setColor(randomColor());
g.drawLine(x1, y1, x2, y2);
}
}
/**
* 创建图⽚的⽅法
*
* @return
*/
private BufferedImage createImage() {
//创建图⽚缓冲区
BufferedImage image = new BufferedImage(weight, height, BufferedImage.TYPE_INT_RGB);
//获取画笔
Graphics2D g = (Graphics2D) Graphics();
//设置背景⾊随机
g.setColor(new Color(255, 255, r.nextInt(245) + 10));
g.fillRect(0, 0, weight, height);
/
/返回⼀个图⽚
return image;
}
/**
* 获取验证码图⽚的⽅法
*
* @return
*/
public BufferedImage getImage() {
BufferedImage image = createImage();
Graphics2D g = (Graphics2D) Graphics(); //获取画笔
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) //画四个字符即可
{
String s = randomChar() + ""; //随机⽣成字符,因为只有画字符串的⽅法,没有画字符的⽅法,所以需要将字符变成字符串再画 sb.append(s); //添加到StringBuilder⾥⾯
float x = i * 1.0F * weight / 4; //定义字符的x坐标
g.setFont(randomFont()); //设置字体,随机
g.setColor(randomColor()); //设置颜⾊,随机
g.drawString(s, x, height - 5);
}
< = sb.toString();
drawLine(image);
return image;
}
/**
* 获取验证码⽂本的⽅法
*
* @return
*/
public String getText() {
return text;
}
public static void output(BufferedImage image, OutputStream out) throws IOException //将验证码图⽚写出的⽅法
{
ImageIO.write(image, "JPEG", out);
ImageIO.write(image, "JPEG", out); }
}
效果查看:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论