JAVA驗證碼工具類

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;
  }

}

發佈了33 篇原創文章 · 獲贊 37 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章