最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2018年05月25日 08:33:37   作者:whyalwaysmea  
這篇文章主要介紹了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ù)生成圖片

/**
 * 生成圖形驗證碼
 * @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)中

@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());
 }
}

在認(rèn)證流程中加入圖形驗證碼

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

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

@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之前:

@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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于Java SSM實現(xiàn)在線點餐系統(tǒng)

    基于Java SSM實現(xiàn)在線點餐系統(tǒng)

    本項目基于Java SSM框架實現(xiàn)在線點餐系統(tǒng),主要實現(xiàn)系統(tǒng)的在線點餐功能。文中的示例代碼講解詳細(xì),感興趣的可以跟隨小編一起學(xué)習(xí)一下
    2022-02-02
  • Spring Boot中使用activiti的方法教程(一)

    Spring Boot中使用activiti的方法教程(一)

    最近一直研究springboot,下面這篇文章主要給大家介紹了關(guān)于Spring Boot中使用activiti的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • Springboot?@Async多線程獲取返回值方式

    Springboot?@Async多線程獲取返回值方式

    這篇文章主要介紹了Springboot?@Async多線程獲取返回值方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Spring?Boot?RestController接口輸出到終端的操作代碼

    Spring?Boot?RestController接口輸出到終端的操作代碼

    這篇文章主要介紹了Spring?Boot?RestController接口如何輸出到終端,使用?HttpServletResponse?類,可以在使用curl執(zhí)行?Spring?Boot?REST接口的同時,在控制臺輸出一些信息,給運維人員知道當(dāng)前命令執(zhí)行的狀態(tài),感興趣的朋友跟隨小編一起看看吧
    2023-09-09
  • Java設(shè)計模式中的門面模式詳解

    Java設(shè)計模式中的門面模式詳解

    門面模式又叫外觀模式(Facade Pattern),主要用于隱藏系統(tǒng)的復(fù)雜性,并向客戶端提供了一個客戶端可以訪問系統(tǒng)的接口,本文通過實例代碼給大家介紹下java門面模式的相關(guān)知識,感興趣的朋友一起看看吧
    2022-09-09
  • JVM解密之解構(gòu)類加載與GC垃圾回收機制詳解

    JVM解密之解構(gòu)類加載與GC垃圾回收機制詳解

    本文主要介紹了Java虛擬機(JVM)的內(nèi)存劃分、類加載機制以及垃圾回收機制,JVM的內(nèi)存劃分為程序計數(shù)器、棧、堆和方法區(qū),類加載機制包括類加載過程、加載器模型和雙親委派模型,垃圾回收機制主要包括標(biāo)記-清除、標(biāo)記-復(fù)制、標(biāo)記-整理和分代回收算法
    2025-02-02
  • Java中使用裝飾設(shè)計模式實現(xiàn)動態(tài)增強對象功能

    Java中使用裝飾設(shè)計模式實現(xiàn)動態(tài)增強對象功能

    裝飾設(shè)計模式是Java中一種常用的設(shè)計模式,它通過動態(tài)地將功能透明地附加到對象上,以擴展對象的功能。裝飾設(shè)計模式主要應(yīng)用于需要動態(tài)、透明地增強對象功能的場景。在Java中,裝飾設(shè)計模式可通過繼承、接口和代理等方式實現(xiàn)
    2023-04-04
  • SpringBoot如何接收數(shù)組參數(shù)的方法

    SpringBoot如何接收數(shù)組參數(shù)的方法

    這篇文章主要介紹了SpringBoot如何接收數(shù)組參數(shù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • MyBatis與Hibernate等ORM框架的區(qū)別及說明

    MyBatis與Hibernate等ORM框架的區(qū)別及說明

    MyBatis和Hibernate是Java中流行的ORM框架,各有特點:MyBatis采用半自動SQL映射,提供細(xì)粒度的SQL控制,適合復(fù)雜查詢和性能優(yōu)化;Hibernate采用全自動對象關(guān)系映射,提供較高的開發(fā)效率,適合簡單的CRUD操作,選擇哪種框架應(yīng)根據(jù)項目需求、團隊技術(shù)棧和個人偏好來決定
    2025-03-03
  • java文件刪除不了的坑,特別是壓縮文件問題

    java文件刪除不了的坑,特別是壓縮文件問題

    這篇文章主要介紹了java文件刪除不了的坑,特別是壓縮文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06

最新評論

永平县| 崇州市| 海盐县| 仙居县| 延津县| 怀柔区| 铁力市| 新乡县| 乃东县| 南木林县| 晋城| 济南市| 和政县| 西贡区| 内乡县| 星子县| 盐边县| 合山市| 德安县| 密云县| 临武县| 大冶市| 肇州县| 弥勒县| 太原市| 平武县| 健康| 克山县| 台中县| 阳山县| 于田县| 文登市| 保康县| 浏阳市| 潢川县| 西安市| 宁陕县| 永平县| 武山县| 屏南县| 阿巴嘎旗|