1.验证码生成类
package com.shop.core.utils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
/**
* 验证码生成类
*
*/
public class Scaptcha {
// 图片的宽度。
private int width = 72;
// 图片的高度。
private int height = 32;
// 验证码字符个数
private int codeCount = 4;
// 验证码干扰线数
private int lineCount = 50;
// 验证码
private String code = null;
// 验证码图片Buffer
private BufferedImage buffImg = null;
private char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };
// 生成随机数
private Random random = new Random();
public Scaptcha() {
this.createCode();
}
/**
*
* @param width 图片宽
* @param height 图片高
*/
public Scaptcha(int width, int height) {
this.width = width;
this.height = height;
this.createCode();
}
/**
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param lineCount 干扰线条数
*/
public Scaptcha(int width, int height, int codeCount, int lineCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
this.createCode();
}
public void createCode() {
int codeX = 0;
int fontHeight = 0;
fontHeight = height - 5;// 字体的高度
codeX = width / (codeCount + 3);// 每个字符的宽度
// 图像buffer
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
// 将图像填充为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// 创建字体
ImgFontByte imgFont = new ImgFontByte();
Font font = imgFont.getFont(fontHeight);
g.setFont(font);
StringBuffer randomCode = new StringBuffer();
// 随机产生验证码字符
for (int i = 0; i < codeCount; i++) {
String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
// 设置字体颜色
g.setColor(getRandomColor());
// 设置字体位置
g.drawString(strRand, (i + 1) * codeX, getRandomNumber(height / 2) + 25);
randomCode.append(strRand);
}
code = randomCode.toString();
}
/** 获取随机颜色 */
private Color getRandomColor() {
int r = getRandomNumber(255);
int g = getRandomNumber(255);
int b = getRandomNumber(255);
return new Color(r, g, b);
}
/** 获取随机数 */
private int getRandomNumber(int number) {
return random.nextInt(number);
}
public void write(String path) throws IOException {
OutputStream sos = new FileOutputStream(path);
this.write(sos);
}
public void write(OutputStream sos) throws IOException {
ImageIO.write(buffImg, "png", sos);
sos.close();
}
public BufferedImage getBuffImg() {
return buffImg;
}
public String getCode() {
return code;
}
/** 字体样式类 */
class ImgFontByte {
public Font getFont(int fontHeight) {
try {
Font baseFont = Font.createFont(Font.HANGING_BASELINE, new ByteArrayInputStream(
hex2byte(getFontByteStr())));
return baseFont.deriveFont(Font.PLAIN, fontHeight);
} catch (Exception e) {
return new Font("Arial", Font.PLAIN, fontHeight);
}
}
private byte[] hex2byte(String str) {
if (str == null)
return null;
str = str.trim();
int len = str.length();
if (len == 0 || len % 2 == 1)
return null;
byte[] b = new byte[len / 2];
try {
for (int i = 0; i < str.length(); i += 2) {
b[i / 2] = (byte) Integer.decode("0x" + str.substring(i, i + 2)).intValue();
}
return b;
} catch (Exception e) {
return null;
}
}
// 字体文件的十六进制字符串
private String getFontByteStr() {
//防止报字符串长度过长错误,改为从配置文件读取
return ReadFontByteProperties.getFontByteStr();
}
}
}
2.生成验证码之读取字体文件类
package com.shop.core.utils;
import java.io.InputStream;
import java.util.Properties;
//生成验证码之读取字体文件类
public class ReadFontByteProperties {
static private String fontByteStr = null;
static {
loads();
}
synchronized static public void loads() {
if (fontByteStr == null) {
InputStream is = ReadFontByteProperties.class.getResourceAsStream("/fontByte.properties");
Properties dbproperties = new Properties();
try {
dbproperties.load(is);
fontByteStr = dbproperties.getProperty("fontByteStr").toString();
} catch (Exception e) {
//System.err.println("不能读取属性文件. " + "请确保fontByte.properties在CLASSPATH指定的路径中");
}
}
}
public static String getFontByteStr() {
if (fontByteStr == null)
loads();
return fontByteStr;
}
}
3.生成图片验证码
/**
* @author jiaqing.xu@hand-china.com
* @date 2017/8/23
* @description 生成图片验证码
*/
@RequestMapping(value = "/verificationCode", method = {RequestMethod.POST, RequestMethod.GET})
@ResponseBody
public void verification(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
// 设置响应的类型格式为图片格式
response.setContentType("image/jpeg");
// 禁止图像缓存。
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
//实例生成验证码对象
Scaptcha instance = new Scaptcha();
//将验证码存入session
session.setAttribute("verification", instance.getCode());
//向页面输出验证码图片
System.out.println(response.getOutputStream());
instance.write(response.getOutputStream());
}
3.将验证码读取到页面上
4.切换验证码
5.验证验证码是否正确,最好到后台验证,要不然前台读取的验证码是上一次的验证码
//验证登陆
function yan(){
/* var nickname=$("#nickname").val();
var password=$("#password").val(); */
var code=$("#code").val();
/* if(nickname.length<=0||password<=0) */
$.ajax({
url : "${pageContext.request.contextPath}/isCode",
type : "post",
data :JSON.stringify({one:code}),
contentType : "application/json;charset=UTF-8",
dataType : "json",
success : function(data){
if(data==1)
{
window.alert("验证码正确");
}else
{
window.alert("验证码错误");
}
}
});
return false;
}
6.
//验证验证码是否正确
@ResponseBody
@RequestMapping(value="/isCode")
public String isCode(@RequestBody Model model,HttpServletRequest request, HttpServletResponse response, HttpSession session){
System.out.println(model.toString());
System.out.println(session.getAttribute("verification"));
if(model.getOne().equals(session.getAttribute("verification"))){
return "1";
}else{
return "0";
}
}