验证码 · fei5156/java_component@b61de46 · GitHub
Skip to content

Commit b61de46

Browse files
committed
验证码
1 parent c7c1d3e commit b61de46

10 files changed

Lines changed: 10259 additions & 0 deletions

File tree

chapter6_verifycode/pom.xml

Lines changed: 64 additions & 0 deletions
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.critc.verify.controller;
2+
3+
import com.critc.verify.util.RandomCodeUtil;
4+
import com.critc.verify.util.VerifyCodeUtil;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.servlet.ModelAndView;
8+
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
12+
/**
13+
* Author 孔垂云
14+
* Date 2017/5/24.
15+
*/
16+
@Controller
17+
public class VerifyController {
18+
/**
19+
* 进入界面
20+
*
21+
* @return
22+
*/
23+
@RequestMapping("/verify")
24+
public ModelAndView verify() {
25+
ModelAndView mv = new ModelAndView();
26+
mv.setViewName("verify");
27+
return mv;
28+
}
29+
30+
/**
31+
* 生成图形验证码
32+
*
33+
* @param request
34+
* @param response
35+
* @param username
36+
* @throws Exception
37+
*/
38+
@RequestMapping(value = "/generateVerifyCode")
39+
public void generateVerifyCode(HttpServletRequest request, HttpServletResponse response, String username) throws Exception {
40+
String randomCode = RandomCodeUtil.createRandomNum(4);//生成四位随机数
41+
response.setContentType("image/jpeg");
42+
//禁止图像缓存。
43+
response.setHeader("Pragma", "no-cache");
44+
response.setHeader("Cache-Control", "no-cache");
45+
response.setDateHeader("Expires", 0);
46+
VerifyCodeUtil vCode = new VerifyCodeUtil(120, 40, 4, 100, randomCode);
47+
request.getSession().setAttribute("verifyCode", randomCode);//放入到session中
48+
vCode.write(response.getOutputStream());
49+
}
50+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.critc.verify.util;
2+
3+
import java.awt.*;
4+
import java.io.ByteArrayInputStream;
5+
6+
/**
7+
* Author 孔垂云
8+
* Date 2017/5/24.
9+
*
10+
*/
11+
public class ImgFontByte {
12+
public Font getFont(int fontHeight) {
13+
try {
14+
Font baseFont = Font.createFont(Font.TRUETYPE_FONT, new ByteArrayInputStream(hex2byte(getFontByteStr())));
15+
return baseFont.deriveFont(Font.PLAIN, fontHeight);
16+
} catch (Exception e) {
17+
return new Font("Arial", Font.PLAIN, fontHeight);
18+
}
19+
}
20+
21+
private byte[] hex2byte(String str) {
22+
if (str == null)
23+
return null;
24+
str = str.trim();
25+
int len = str.length();
26+
if (len == 0 || len % 2 == 1)
27+
return null;
28+
29+
byte[] b = new byte[len / 2];
30+
try {
31+
for (int i = 0; i < str.length(); i += 2) {
32+
b[i / 2] = (byte) Integer.decode("0x" + str.substring(i, i + 2)).intValue();
33+
}
34+
return b;
35+
} catch (Exception e) {
36+
return null;
37+
}
38+
}
39+
40+
/**
41+
* ttf字体文件的十六进制字符串
42+
* @return
43+
*/
44+
private String getFontByteStr() {
45+
return null;
46+
}
47+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.critc.verify.util;
2+
3+
import java.util.Random;
4+
5+
/**
6+
*
7+
* 功能描述:随机数生成工具
8+
*
9+
* @version 1.0.0
10+
* @author 孔垂云
11+
* @2015年3月4日
12+
*/
13+
public class RandomCodeUtil {
14+
private static final char[] codeSequenceRandom = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4',
15+
'5', '6', '7', '8', '9', '0' };
16+
private static final char[] numberSequenceRandom = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
17+
18+
/**
19+
*
20+
* 功能描述:生成字符串随机数
21+
*
22+
* @return String
23+
* @version 1.0.0
24+
* @author 孔垂云
25+
*/
26+
public static String createRandomCode(int count) {
27+
String serialNum = "";
28+
Random random = new Random();
29+
for (int i = 0; i < count; i++) {
30+
String strRand = String.valueOf(codeSequenceRandom[random.nextInt(codeSequenceRandom.length)]);
31+
serialNum += strRand;
32+
}
33+
return serialNum;
34+
}
35+
36+
/**
37+
* 生成数字随机数
38+
* @param count
39+
* @return
40+
*/
41+
public static String createRandomNum(int count) {
42+
String serialNum = "";
43+
Random random = new Random();
44+
for (int i = 0; i < count; i++) {
45+
String strRand = String.valueOf(numberSequenceRandom[random.nextInt(numberSequenceRandom.length)]);
46+
serialNum += strRand;
47+
}
48+
return serialNum;
49+
}
50+
51+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.critc.verify.util;
2+
3+
import javax.imageio.ImageIO;
4+
import java.awt.*;
5+
import java.awt.image.BufferedImage;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.io.OutputStream;
9+
import java.util.Random;
10+
11+
/**
12+
* 验证码工具类
13+
*/
14+
public class VerifyCodeUtil {
15+
// 图片的宽度。
16+
private int width = 120;
17+
// 图片的高度。
18+
private int height = 40;
19+
// 验证码字符个数
20+
private int codeCount = 5;
21+
// 验证码干扰线数
22+
private int lineCount = 150;
23+
// 验证码
24+
private String code = null;
25+
// 验证码图片Buffer
26+
private BufferedImage buffImg = null;
27+
28+
public VerifyCodeUtil(String randomCode) {
29+
this.createCode(randomCode);
30+
}
31+
32+
/**
33+
* @param width 图片宽
34+
* @param height 图片高
35+
* @param randomCode 随机数
36+
*/
37+
public VerifyCodeUtil(int width, int height, String randomCode) {
38+
this.width = width;
39+
this.height = height;
40+
this.createCode(randomCode);
41+
}
42+
43+
/**
44+
* @param width 图片宽
45+
* @param height 图片高
46+
* @param codeCount 字符个数
47+
* @param lineCount 干扰线条数
48+
* @param randomCode 随机数
49+
*/
50+
public VerifyCodeUtil(int width, int height, int codeCount, int lineCount, String randomCode) {
51+
this.width = width;
52+
this.height = height;
53+
this.codeCount = codeCount;
54+
this.lineCount = lineCount;
55+
this.createCode(randomCode);
56+
}
57+
58+
public void createCode(String randomCode) {
59+
int x = 0, fontHeight = 0, codeY = 0;
60+
int red = 0, green = 0, blue = 0;
61+
62+
x = width / (codeCount + 2);//每个字符的宽度
63+
fontHeight = height - 2;//字体的高度
64+
codeY = height - 4;
65+
66+
// 图像buffer
67+
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
68+
Graphics2D g = buffImg.createGraphics();
69+
// 生成随机数
70+
Random random = new Random();
71+
// 将图像填充为白色
72+
g.setColor(Color.WHITE);
73+
g.fillRect(0, 0, width, height);
74+
// 创建字体
75+
ImgFontByte imgFont = new ImgFontByte();
76+
Font font = imgFont.getFont(fontHeight);
77+
g.setFont(font);
78+
79+
for (int i = 0; i < lineCount; i++) {
80+
int xs = random.nextInt(width);
81+
int ys = random.nextInt(height);
82+
int xe = xs + random.nextInt(width / 8);
83+
int ye = ys + random.nextInt(height / 8);
84+
red = random.nextInt(255);
85+
green = random.nextInt(255);
86+
blue = random.nextInt(255);
87+
g.setColor(new Color(red, green, blue));
88+
g.drawLine(xs, ys, xe, ye);
89+
}
90+
91+
// randomCode记录随机产生的验证码
92+
// StringBuffer randomCode = new StringBuffer();
93+
// 随机产生codeCount个字符的验证码。
94+
for (int i = 0; i < randomCode.length(); i++) {
95+
String strRand = randomCode.substring(i, i + 1);
96+
// 产生随机的颜色值,让输出的每个字符的颜色值都将不同。
97+
red = random.nextInt(255);
98+
green = random.nextInt(255);
99+
blue = random.nextInt(255);
100+
g.setColor(new Color(red, green, blue));
101+
g.drawString(strRand, (i + 1) * x, codeY);
102+
// 将产生的四个随机数组合在一起。
103+
}
104+
}
105+
106+
public void write(String path) throws IOException {
107+
OutputStream sos = new FileOutputStream(path);
108+
this.write(sos);
109+
}
110+
111+
public void write(OutputStream sos) throws IOException {
112+
ImageIO.write(buffImg, "png", sos);
113+
sos.close();
114+
}
115+
116+
public BufferedImage getBuffImg() {
117+
return buffImg;
118+
}
119+
120+
public String getCode() {
121+
return code;
122+
}
123+
124+
public static void main(String[] args) {
125+
VerifyCodeUtil vCode = new VerifyCodeUtil(120, 40, 5, 100, "11111");
126+
try {
127+
String path = "D:/t/" + "11.png";
128+
System.out.println(vCode.getCode() + " >" + path);
129+
vCode.write(path);
130+
} catch (IOException e) {
131+
e.printStackTrace();
132+
}
133+
}
134+
}
Lines changed: 28 additions & 0 deletions

0 commit comments

Comments
 (0)