本文实例为大家分享了springboot实现验证码登录的具体代码,供大家参考,具体内容如下
因为在项目中需要使用到验证码,我总结一下在项目中如何快速解决项目需求~验证码,下面推荐给大家速上手验证码的例子。
一、编写验证码工具类
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
|
import java.awt.color;
import java.awt.font;
import java.awt.graphics;
import java.awt.image.bufferedimage;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.outputstream;
import java.util.random;
import javax.imageio.imageio;
/**
* @author zct
* @date 2018年2月6日
* @param
* @desc 图形验证码生成
*
*/
public class verifyutil {
// 验证码字符集
private static final char [] chars = {
'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ,
'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' ,
'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' };
// 字符数量
private static final int size = 4 ;
// 干扰线数量
private static final int lines = 5 ;
// 宽度
private static final int width = 80 ;
// 高度
private static final int height = 40 ;
// 字体大小
private static final int font_size = 30 ;
/**
* 生成随机验证码及图片
* object[0]:验证码字符串;
* object[1]:验证码图片。
*/
public static object[] createimage() {
stringbuffer sb = new stringbuffer();
// 1.创建空白图片
bufferedimage image = new bufferedimage(
width, height, bufferedimage.type_int_rgb);
// 2.获取图片画笔
graphics graphic = image.getgraphics();
// 3.设置画笔颜色
graphic.setcolor(color.light_gray);
// 4.绘制矩形背景
graphic.fillrect( 0 , 0 , width, height);
// 5.画随机字符
random ran = new random();
for ( int i = 0 ; i <size; i++) {
// 取随机字符索引
int n = ran.nextint(chars.length);
// 设置随机颜色
graphic.setcolor(getrandomcolor());
// 设置字体大小
graphic.setfont( new font(
null , font.bold + font.italic, font_size));
// 画字符
graphic.drawstring(
chars[n] + "" , i * width / size, height* 2 / 3 );
// 记录字符
sb.append(chars[n]);
}
// 6.画干扰线
for ( int i = 0 ; i < lines; i++) {
// 设置随机颜色
graphic.setcolor(getrandomcolor());
// 随机画线
graphic.drawline(ran.nextint(width), ran.nextint(height),
ran.nextint(width), ran.nextint(height));
}
// 7.返回验证码和图片
return new object[]{sb.tostring(), image};
}
/**
* 随机取色
*/
public static color getrandomcolor() {
random ran = new random();
color color = new color(ran.nextint( 256 ),
ran.nextint( 256 ), ran.nextint( 256 ));
return color;
}
}
|
二、controller层使用
验证用户名和密码和验证码一致
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
|
/**
* 登录入口
*
* @param username 用户名
* @param password 密码
* @param code 验证码
* @param response 回调json数据 成功返回200,失败返回500
*/
@apioperation ( "登录" )
@postmapping ( "/login" )
public void adminloginbypasswword( @apiparam ( "用户名" ) @requestparam string username, @apiparam ( "密码" ) @requestparam string password, @apiparam ( "验证码" ) @requestparam string code, httpservletresponse response,httpservletrequest request) {
httpsession session=request.getsession();
if (session.getattribute( "imagecode" )== null ){
renderfail(response, "重新获取验证码" );
} else {
if (session.getattribute( "imagecode" ).tostring().equalsignorecase(code)){
map<string, object> user = adminservice.checkadminlogin(username, password);
if (user == null ) {
renderfail(response, "登录失败" );
} else {
rendersuccess(response, "登录成功" );
}
} else {
renderfail(response, "验证码错误" );
}
}
}
|
这里采用get请求获取验证码,获取验证码的接口如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@apioperation ( "生成验证码" )
@getmapping ( "/getcode" )
public void getcode(httpservletresponse response, httpservletrequest request) throws exception{
httpsession session=request.getsession();
//利用图片工具生成图片
//第一个参数是生成的验证码,第二个参数是生成的图片
object[] objs = verifyutil.createimage();
//将验证码存入session
session.setattribute( "imagecode" ,objs[ 0 ]);
//将图片输出给浏览器
bufferedimage image = (bufferedimage) objs[ 1 ];
response.setcontenttype( "image/png" );
outputstream os = response.getoutputstream();
imageio.write(image, "png" , os);
}
|
三、代码测试
这里用springboot swagger2测试
上面是get请求获取验证码,下面是登录验证,验证结果是成功的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/zct1115/article/details/79302434