本文實例為大家分享了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