Spring mvc 的服務器端圖形驗證碼~~
如下代碼是將圖形驗證碼的代碼用spring mvc寫了一下
非常不錯的基礎圖形驗證碼~
-----------------------
@RestController
@RequestMapping("")
public class ImgCodeController {
@Autowired
RedisTemplate<String,String> cache;
private int width = 90;//定義圖片的width
private int height = 30;//定義圖片的height
private int codeCount = 4;//定義圖片上顯示驗證碼的個數
private int xx = 15;
private int fontHeight = 18;
private int codeY = 16;
char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
@RequestMapping("/checkCode")
protected void doGetCode(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
//設置頁面不緩存
httpServletResponse.setHeader("Pragma", "No-cache");
httpServletResponse.setHeader("Cache-Control", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
// 在內存中創建圖象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 獲取圖形上下文
Graphics g = image.getGraphics();
// 創建一個隨機數生成器類
Random random = new Random();
// 將圖像填充為白色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
//設定字體
g.setFont(new Font("Times New Roman", Font.PLAIN, fontHeight));
//畫邊框
g.setColor(new Color(20,20,20));
g.drawRect(0, 0, width - 1, height - 1);
// 隨機產生155條干擾線,使圖象中的認證碼不易被其它程序探測到
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
// randomCode用於保存隨機產生的驗證碼,以便用戶登錄後進行驗證。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
// 隨機產生codeCount數字的驗證碼。
for (int i = 0; i < codeCount; i++) {
// 得到隨機產生的驗證碼數字。
String code = String.valueOf(codeSequence[random.nextInt(36)]);
// 產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。
red = 20 + random.nextInt(110);
green = 20 + random.nextInt(110);
blue = 20 + random.nextInt(110);
// 用隨機產生的顏色將驗證碼繪制到圖像中。
g.setColor(new Color(red, green, blue));
g.drawString(code, (i + 1) * xx, codeY);
// 將產生的四個隨機數組合在一起。
randomCode.append(code);
}
// 將四位數字的驗證碼保存到Session中。
HttpSession session = httpServletRequest.getSession();
// 圖象生效
g.dispose();
session.setAttribute("code", randomCode.toString());
// 將圖像輸出到Servlet輸出流中。
ServletOutputStream sos = httpServletResponse.getOutputStream();
ImageIO.write(image, "jpeg", sos);
sos.close();
}
/**
* 獲取隨機顏色
* @param fc
* @param bc
* @return
*/
private Color getRandColor(int fc,int bc) {
Random random = new Random();
if(fc > 255) {
fc = 255;
}
if(bc > 255) {
bc = 255;
}
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getCodeCount() {
return codeCount;
}
public void setCodeCount(int codeCount) {
this.codeCount = codeCount;
}
public int getXx() {
return xx;
}
public void setXx(int xx) {
this.xx = xx;
}
public int getFontHeight() {
return fontHeight;
}
public void setFontHeight(int fontHeight) {
this.fontHeight = fontHeight;
}
public int getCodeY() {
return codeY;
}
public void setCodeY(int codeY) {
this.codeY = codeY;
}
}
*