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.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jinsuicloud.api.web.common.formbean.CheckCodeForm;
import com.jinsuicloud.api.web.common.vo.ResultBean;
/**
* 验证码
*/
@Controller
@RequestMapping("/HiddenDanger/check")
public class CodeController {
private int width = 90;//定义图片的width
private int height = 36;//定义图片的height
private int codeCount = 4;//定义图片上显示验证码的个数
private int xx = 16;
private int fontHeight = 28;
private int codeY = 26;
char[] codeSequence = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
@RequestMapping("/code")
public void getCode(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
// 定义图像buffer
BufferedImage buffImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// Graphics2D gd = buffImg.createGraphics();
//Graphics2D gd = (Graphics2D) buffImg.getGraphics();
Graphics gd = buffImg.getGraphics();
// 创建一个随机数生成器类
Random random = new Random();
Color backColor = new Color(4,34,88);
Color fontColor = new Color(88,255,255);
System.out.println(Color.WHITE);
//图像填充颜色
gd.setColor(backColor);
//gd.fillPolygon(new int[]{0,width,width,width-10,0,0}, new int[]{0,0,height-10,height,height,0}, 6);
gd.fillRect(0, 0, width, height);
// 创建字体,字体的大小应该根据图片的高度来定。
Font font = new Font("Fixedsys", Font.ITALIC, fontHeight);
// 设置字体。
gd.setFont(font);
// 画边框。
gd.setColor(fontColor);
//gd.fillPolygon(new int[]{0,width,width,width-10,0,0}, new int[]{0,0,height-10,height,height,0}, 6);
gd.drawPolygon(new int[]{0,width-16,width-1,width-1,0,0}, new int[]{0,0,height-24,height-1,height-1,0}, 6);
// gd.drawRect(0, 0, width - 1, height - 1);
// 随机产生40条干扰线,使图象中的认证码不易被其它程序探测到。
gd.setColor(fontColor);
for (int i = 0; i < 5; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(15);
int yl = random.nextInt(15);
gd.drawLine(x, y, x + xl, y + yl);
}
// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBuffer randomCode = new StringBuffer();
// 随机产生codeCount数字的验证码。
for (int i = 0; i < codeCount; i++) {
// 得到随机产生的验证码数字。
String code = String.valueOf(codeSequence[random.nextInt(10)]);
gd.setColor(fontColor);
gd.drawString(code, (i + 1) * xx, codeY);
// 将产生的四个随机数组合在一起。
randomCode.append(code);
}
// 将四位数字的验证码保存到Session中。
HttpSession session = req.getSession();
System.err.print("验证码----------------------》"+randomCode);
session.setMaxInactiveInterval(60*60);//60秒
session.setAttribute("code", randomCode.toString());
// 禁止图像缓存。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
// 将图像输出到Servlet输出流中。
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "JPEG", sos);
sos.close();
}
/**
* 查询表
*
* @return
*/
@ResponseBody
@RequestMapping("/yzCode")
public ResultBean yzCode(@RequestBody CheckCodeForm form,HttpSession session) {
ResultBean rb=new ResultBean();
rb.setMsg("验证码正确");
rb.setCode(1);
String code= (String) session.getAttribute("code");
System.err.println("session --------->"+code +" code=: "+form.getCode());
if (code==null||!code.equalsIgnoreCase(form.getCode())) {
rb.setCode(-1);
rb.setMsg("验证码不正确!");
}
return rb;
}
}
JAVA验证码工具类
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.