package com.zdsoft.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 验证码Controller
 * 
 * @author yangtao
 *
 */
@Controller
@RequestMapping("/safecode")
public class SafaCodeController {
	/**
	 * 将safecode的字符串放入session
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/create_sc", method = RequestMethod.GET)
	public void validSafeCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 设置响应头 Content-type类型
		response.setContentType("image/jpeg");
		// 以下三句是用于设置页面不缓存
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "No-cache");
		response.setDateHeader("Expires", 0);

		OutputStream os = response.getOutputStream();
		int width = 80, height = 20;
		// 建立指定宽、高和BufferedImage对象
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

		Graphics g = image.getGraphics(); // 该画笔画在image上
		Color c = g.getColor(); // 保存当前画笔的颜色,用完画笔后要回复现场
		g.fillRect(0, 0, width, height);
		// 设定字体
		g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

		char[] ch = "abcdefghjkmnpqrstuvwxyz23456789".toCharArray(); // 随即产生的字符串
		int length = ch.length; // 随即字符串的长度
		String sRand = ""; // 保存随即产生的字符串
		Random random = new Random();
		for (int i = 0; i < 6; i++) {
			String rand = new Character(ch[random.nextInt(length)]).toString();
			sRand += rand;
			// 将认证码显示到图象中
			g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
			// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
			g.drawString(rand, 13 * i + 6, 16);
		}
		g.setColor(c); // 将画笔的颜色再设置回去
		g.dispose();

		// 将验证码记录到session
		request.getSession().setAttribute("safecode", sRand);
		// 输出图像到页面
		ImageIO.write(image, "JPEG", os);
	}

	/**
	 * Ajax判断用户输入的验证码是否有效
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/check_sc", method = RequestMethod.POST)
	public void safeCodeCheck(HttpServletRequest request, HttpServletResponse response) {
		// 指定输出内容类型和编码
		response.setContentType("text/html;charset=utf-8");
		// 获取到用户通过ajax传过来的验证码
		String safecode = request.getParameter("safecode").toLowerCase();
		try {
			PrintWriter out = response.getWriter(); // 获取输出流
			boolean isValid = false; // 验证码是否有效标识
			if (request.getSession().getAttribute("safecode").equals(safecode)) {
				isValid = true;
			}
			out.print(isValid);// 返回结果
			out.flush();
			out.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
最近下载更多
微信网友_6248713511227392  LV11 2022年12月5日
stdtta  LV8 2022年6月28日
543666826  LV33 2021年11月20日
CSS199669  LV25 2020年7月26日
wanminhang  LV12 2020年4月9日
李兰香  LV11 2019年12月16日
p13994295637  LV7 2019年12月12日
aaabbc  LV9 2019年12月4日
乱邪君  LV6 2019年11月26日
仗剑江湖wzl  LV11 2019年10月24日
最近浏览更多
xuweiwowzy  LV5 1月14日
wwwww816  LV3 2023年12月19日
sunshine9920  LV12 2023年11月6日
李亮  LV19 2023年8月29日
gecongkai  LV8 2023年6月22日
c469893928  LV2 2023年6月10日
liyc_ayer  LV1 2023年5月30日
小帅哥  LV2 2023年5月7日
uni-code_0123  LV1 2023年3月23日
sweetbox  LV10 2023年2月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友