激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

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

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

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

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

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

生成圖形驗證碼

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

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

根據(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中 && 將生成的圖片寫到接口的響應中

?
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. 如果校驗不通過,調用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()     // 定義當需要用戶登錄時候,轉到的登錄頁面。
   // 后面的配置省略
}   

代碼下載

spring-security

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美a区 | 啊~用cao嗯力cao烂我视频 | 天天干天天透 | 日本黄色免费片 | 久久精品亚洲欧美日韩精品中文字幕 | 日本欧美在线播放 | 久久精品亚洲欧美日韩精品中文字幕 | 欧美成人一级视频 | 男女隐私免费视频 | 黄色成人短视频 | 国产青草网 | 亚洲免费观看视频 | 91精品国产九九九久久久亚洲 | china对白普通话xxxx | 欧美一级一片 | 又黄又爽免费无遮挡在线观看 | 91看片淫黄大片欧美看国产片 | 久草最新 | 欧美精品免费一区二区三区 | 麻豆视频在线观看免费网站 | 免费国产人成网站 | 免费国产之a视频 | 福利国产视频 | 天天操天天骑 | 99国产精品国产免费观看 | 欧美日韩一区三区 | 黄色网址免费在线播放 | 国产一有一级毛片视频 | 第一福利在线 | 久久成人黄色 | 91久久另类重口变态 | 国产一精品一av一免费爽爽 | 欧美不卡在线 | 久久国产精品二区 | 欧美成人高清视频 | 老a影视网站在线观看免费 欧美日韩成人一区二区 | 欧美 中文字幕 | 免费在线观看一级片 | av免费在线观 | 成人羞羞网站入口 | 日本中文字幕电影在线观看 |