spring boot是由pivotal團隊提供的全新框架,其設計目的是用來簡化新spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,spring boot致力于在蓬勃發展的快速應用開發領域(rapid application development)成為領導者。
下面通過實例代碼給大家介紹spring boot實現驗證碼功能,具體詳情如下所示:
1.建立工具類,配置驗證碼相關參數
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
|
import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.image.bufferedimage; import java.util.random; /** * @author ld * @date 2017年11月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; } } |
2.接口
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@requestmapping (value= "/createvalicode" ,method=requestmethod.get) public void valicode(httpservletresponse response,httpsession session) throws exception{ //利用圖片工具生成圖片 //第一個參數是生成的驗證碼,第二個參數是生成的圖片 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); } |
3.測試頁面調用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<!doctype html> <html> <head lang= "en" > <meta charset= "utf-8" /> <title>hello</title> </head> <body> <h1 th:text= "${info}" /> <div> <!-- <img alt= "這是圖片" src= "/img/001.png" /> --> <img alt= "驗證碼" onclick = "this.src='/iot-frame/createvalicode?' + math.floor(math.random() * 100)" src= "/iot-frame/createvalicode" /> </div> <form action= "imgvrifycontrollerdefaultkaptcha" > <input type= "text" name= "vrifycode" /> <input type= "submit" value= "提交" ></input> </form> </body> </html> |
總結
以上所述是小編給大家介紹的spring boot實現驗證碼功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://blog.csdn.net/mmp_gaihaoleba/article/details/80005905