avatar

目录
【实现】用户认证:验证码

[toc]

工具类

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package hw.utils.common;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
* 生成验证码
*/
public class ValidateCode {
private int width = 70;
private int height = 35;
private Random random = new Random();
private StringBuilder code = new StringBuilder(4);
//创建一张有背景色的图片
private BufferedImage createImage(){
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(new Color(240,240,240));
g.fillRect(0, 0, width, height);
return img;
}

//此方法必须在getImage方法之后调用
private String getCode(){
return this.code.toString();
}
//获得验证码图片
private BufferedImage getImage(){
//获得有背景色的图片
BufferedImage img = this.createImage();
//绘制字母:颜色随机,字体大小,字体,字号随机,字母随机
Graphics g = img.getGraphics();
for(int i=0;i<4;i++){
String str = this.getRandomString();//得到随机字母
this.code.append(str);
g.setFont(this.getRandomFont());//得到随机字体
g.setColor(this.getRandomColor());//得到随机颜色
g.drawString(str, width/4*i, height-5);
}
//绘制干扰线
this.drawLines(img);
return img;
}

//绘制干扰线
private void drawLines(BufferedImage image){
Graphics g = image.getGraphics();
g.setColor(Color.BLACK);
for(int i=0;i<5;i++){
int x1 = random.nextInt(this.width);
int y1 = random.nextInt(this.height);
int x2 = random.nextInt(this.width);
int y2 = random.nextInt(this.height);
g.drawLine(x1, y1, x2, y2);
}
}
private Color getRandomColor() {
int r = this.random.nextInt(256);//0-255
int g = this.random.nextInt(256);
int b = this.random.nextInt(256);
return new Color(r, g, b);
}

private String[] fontNames = {"宋体","华文楷体","黑体","华文新魏","华文隶书","微软雅黑","楷体_GB2312"};
private int[] fontSizes = {24,25,26,27,28};
private Font getRandomFont() {
int index = this.random.nextInt(this.fontNames.length);//0-6
String name = this.fontNames[index];
int style = this.random.nextInt(4);
index = this.random.nextInt(this.fontSizes.length);
int size = this.fontSizes[index];
return new Font(name, style, size);
}

private String codes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private String getRandomString() {
int index = random.nextInt(this.codes.length());
return this.codes.charAt(index)+"";
}


//保存图片
private void saveImage(BufferedImage im,OutputStream output) throws IOException{
ImageIO.write(im, "JPEG", output);
}

/**
* 将图片输出到页面,并返回验证码
*/
public static String outputImage(HttpServletResponse response) throws Exception{

response.setContentType("image/jpeg");
ServletOutputStream servletOutputStream = response.getOutputStream();
ValidateCode v = new ValidateCode();
BufferedImage img = v.getImage();
//System.out.println(v.getCode());//getCode必须在getImage后面
v.saveImage(img,servletOutputStream);
servletOutputStream.flush();
servletOutputStream.close();

return v.getCode();

}
/*
public static void main(String[] args) throws Exception {

//使用说明
ValidateCode v = new ValidateCode();
BufferedImage img = v.getImage();
System.out.println(v.getCode());//getCode必须在getImage后面
ValidateCode.saveImage(img, new FileOutputStream("/machine/a.jpg"));

}

*/

}

验证码输出到页面

Code
1
2
3
4
5
6
7
8
9
10
11
12
@RequestMapping("/validatecode")
public String validatecode(HttpServletResponse response,
HttpSession session) throws Exception {

//向页面输出验证码,并得到验证码
String validateCode = ValidateCode.outputImage(response);
//把验证码放到session域
session.setAttribute("validateCode",validateCode);

return "/base/login";

}

页面请求验证码

Code
1
2
3
4
5
6
7
8
<TR>
<TD>验证码:</TD>
<TD>
<input id="randomcode" name="randomcode" size="8" />
<img id="randomcode_img" src="${baseurl}login/validatecode.action" alt=""
width="56" height="20" align='absMiddle' /> <a
href=javascript:randomcode_refresh()>刷新</a></TD>
</TR>
Code
1
2
3
4
5
6
//刷新验证码
//实现思路,重新给图片的src赋值,后边加时间,防止缓存
function randomcode_refresh() {
$("#randomcode_img").attr('src',
'${baseurl}login/validatecode.action?time' + new Date().getTime());
}

认证校验

Code
1
2
3
4
5
6
7
8
9
10
11
12
//登录表单提交
@RequestMapping("/loginsubmit")
public String loginformsubmit(HttpSession session) throws Exception {

//从session获取验证码
String validateCode_session = (String)session.getAttribute("validateCode");

//...进行登录校验

return "/base/first";//首页

}
文章作者: Machine
文章链接: https://machine4869.gitee.io/2018/04/20/15326658481209/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 哑舍
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论