一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringBoot結(jié)合SpringSecurity實現(xiàn)圖形驗證碼功能

SpringBoot結(jié)合SpringSecurity實現(xiàn)圖形驗證碼功能

2021-05-05 11:05whyalwaysmea Java教程

這篇文章主要介紹了SpringBoot + SpringSecurity 實現(xiàn)圖形驗證碼功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了springboot結(jié)合springsecurity實現(xiàn)圖形驗證碼功能,分享給大家,具體如下:

生成圖形驗證碼

  1. 根據(jù)隨機數(shù)生成圖片
  2. 將隨機數(shù)存到session中
  3. 將生成的圖片寫到接口的響應(yīng)中

生成圖形驗證碼的過程比較簡單,和springsecurity也沒有什么關(guān)系。所以就直接貼出代碼了

根據(jù)隨機數(shù)生成圖片

?
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
/**
 * 生成圖形驗證碼
 * @param request
 * @return
 */
private imagecode generate(servletwebrequest request) {
 int width = 64;
 int height = 32;
 bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb);
 
 graphics g = image.getgraphics();
 
 random random = new random();
 
 g.setcolor(getrandcolor(200, 250));
 g.fillrect(0, 0, width, height);
 g.setfont(new font("times new roman", font.italic, 20));
 g.setcolor(getrandcolor(160, 200));
 for (int i = 0; i < 155; i++) {
  int x = random.nextint(width);
  int y = random.nextint(height);
  int xl = random.nextint(12);
  int yl = random.nextint(12);
  g.drawline(x, y, x + xl, y + yl);
 }
 
 string srand = "";
 for (int i = 0; i < 4; i++) {
  string rand = string.valueof(random.nextint(10));
  srand += rand;
  g.setcolor(new color(20 + random.nextint(110), 20 + random.nextint(110), 20 + random.nextint(110)));
  g.drawstring(rand, 13 * i + 6, 16);
 }
 
 g.dispose();
 
 return new imagecode(image, srand, 60);
 
}
 
/**
 * 生成隨機背景條紋
 *
 * @param fc
 * @param bc
 * @return
 */
private color getrandcolor(int fc, int bc) {
 random random = new random();
 if (fc > 255) {
  fc = 255;
 }
 if (bc > 255) {
  bc = 255;
 }
 int r = fc + random.nextint(bc - fc);
 int g = fc + random.nextint(bc - fc);
 int b = fc + random.nextint(bc - fc);
 return new color(r, g, b);
}

將隨機數(shù)存到session中 && 將生成的圖片寫到接口的響應(yīng)中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@restcontroller
public class validatecodecontroller {
 
 public static final string session_key = "session_key_image_code";
 
 private sessionstrategy sessionstrategy = new httpsessionsessionstrategy();
 
 @getmapping("/code/image")
 public void createcode(httpservletrequest request, httpservletresponse response) throws ioexception {
  imagecode imagecode = generate(new servletwebrequest(request));
  sessionstrategy.setattribute(new servletwebrequest(request), session_key, imagecode);
  imageio.write(imagecode.getimage(), "jpeg", response.getoutputstream());
 }
}

在認證流程中加入圖形驗證碼

springsecurity認證流程詳解中,我們有講到,springsecurity是通過過濾器鏈來進行校驗的,我們想要驗證圖形驗證碼,所以可以在認證流程之前,也就是usernamepasswordauthenticationfilter之前進行校驗。

自定義圖形驗證碼的過濾器

?
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
@component
public class validatecodefilter extends onceperrequestfilter {
 
 private sessionstrategy sessionstrategy = new httpsessionsessionstrategy();
 
 private authenticationfailurehandler authenticationfailurehandler;
 
 @override
 protected void dofilterinternal(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, filterchain filterchain) throws servletexception, ioexception {
  if(stringutils.equals("/user/login", httpservletrequest.getrequesturi())
    && stringutils.equalsignorecase(httpservletrequest.getmethod(), "post")) {
 
   try {
    // 1. 進行驗證碼的校驗
    validate(new servletwebrequest(httpservletrequest));
   } catch (validatecodeexception e) {
    // 2. 如果校驗不通過,調(diào)用springsecurity的校驗失敗處理器
    authenticationfailurehandler.onauthenticationfailure(httpservletrequest, httpservletresponse, e);
    return ;
   }
  }
  // 3. 校驗通過,就放行
  filterchain.dofilter(httpservletrequest, httpservletresponse);
 }
}

這里驗證碼校驗的過程比較簡單,主要就是判斷傳過來的參數(shù)和session中保存的是否一致,以及session中的驗證碼是否過期了。

有了自己的驗證碼過濾器之后,我們還需要將它配置在usernamepasswordauthenticationfilter之前:

?
1
2
3
4
5
6
7
8
9
@override
protected void configure(httpsecurity http) throws exception {
 validatecodefilter validatecodefilter = new validatecodefilter();
 validatecodefilter.setauthenticationfailurehandler(myauthenticationfailurehandler);
 // 將我們自定義的過濾器,配置到usernamepasswordauthenticationfilter之前
 http.addfilterbefore(validatecodefilter, usernamepasswordauthenticationfilter.class)
   .formlogin()     // 定義當(dāng)需要用戶登錄時候,轉(zhuǎn)到的登錄頁面。
   // 后面的配置省略
}   

代碼下載

spring-security

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/u013435893/article/details/79617872

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 好男人好资源在线观看免费 | 久久青青草原精品国产软件 | 国产成人精品免费午夜 | 国产成人精品1024在线 | 午夜爱情动作片P | 久久成人免费大片 | 日本免费三片在线观看 | 九色PORNY真实丨国产免费 | 乌克兰bbw | 亚洲精品久久久WWW游戏好玩 | 国内揄拍国内精品久久 | 日本中文字幕二区三区 | poren黑人 | 亚洲区在线| 闺蜜的样子小说安沁在线阅读 | 日本艳鉧动漫1~6完整版在 | 亚洲一区二区三区深夜天堂 | avav一区 | 国产成人综合视频 | 日本性爱 | 女人扒开下面让男人桶爽视频 | 放荡警察巨r麻麻出轨小说 范冰冰特黄xx大片 饭冈加奈子在线播放观看 法国老妇性xx在线播放 | 国产精品一区二区国产 | 被调教的校花 | 暖暖高清日本在线 | 欧美三茎同入 | 羞羞漫画免费漫画页面在线看漫画秋蝉 | 久久久亚洲国产精品主播 | 香蕉在线精品亚洲第一区 | 18videossex性欧美69 | chinesemature老女人 | 91免费高清视频 | 天堂va在线高清一区 | 国模一区二区三区视频一 | eeuss18影院www国产 | 久久久久久88色偷偷 | 好男人资源在线观看免费的 | 天堂成人在线 | 成人国产在线视频在线观看 | 校服下的白嫩小乳尖h1v1 | 精品综合一区二区三区 |