欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年05月25日 08:33:37   作者:whyalwaysmea  
這篇文章主要介紹了SpringBoot + SpringSecurity 實現(xiàn)圖形驗證碼功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

生成圖形驗證碼

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

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

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

@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之前進行校驗。

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

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

@Override
protected void configure(HttpSecurity http) throws Exception {
 ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter();
 validateCodeFilter.setAuthenticationFailureHandler(myAuthenticationFailureHandler);
 // 將我們自定義的過濾器,配置到UsernamePasswordAuthenticationFilter之前
 http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
   .formLogin()     // 定義當需要用戶登錄時候,轉到的登錄頁面。
   // 后面的配置省略
}    

代碼下載

Spring-Security

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

相關文章

  • Java背包問題求解實例代碼

    Java背包問題求解實例代碼

    這篇文章主要介紹了Java背包問題求解實例代碼,其中涉及兩種背包:01和完全背包。分別講述了兩種背包的思路和實現(xiàn)方法,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • java8異步調用如何使用才是最好的方式

    java8異步調用如何使用才是最好的方式

    異步調用主要用于當前程序的執(zhí)行不用等待調用方法執(zhí)行結束就可以繼續(xù)執(zhí)行,下面這篇文章主要給大家介紹了關于java8異步調用如何使用才是最好的方式,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-01-01
  • 基于SpringBoot實現(xiàn)輕量級的動態(tài)定時任務調度的方法

    基于SpringBoot實現(xiàn)輕量級的動態(tài)定時任務調度的方法

    本文介紹了如何在SpringBoot框架中實現(xiàn)輕量級的動態(tài)定時任務調度,通過將任務以類為基礎單位,并通過配置數(shù)據(jù)進行任務讀取和反射生成任務對象,感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • 淺談MyBatisPlus中LocalDateTime引發(fā)的一些問題和解決辦法

    淺談MyBatisPlus中LocalDateTime引發(fā)的一些問題和解決辦法

    MyBatisPlus進行數(shù)據(jù)庫操作時,我們經常會遇到處理日期時間類型的需求,本文主要介紹了淺談MyBatisPlus中LocalDateTime引發(fā)的一些問題和解決辦法,具有一定的參考價值,感興趣的可以了解一下
    2024-07-07
  • java實現(xiàn)同步回調的示例代碼

    java實現(xiàn)同步回調的示例代碼

    同步回調是一種在調用代碼中同步執(zhí)行回調函數(shù)的編程模式,在Java中,通過定義和實現(xiàn)接口來構建同步回調,本文就來介紹一下如何實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-09-09
  • java spark文件讀取亂碼問題的解決方法

    java spark文件讀取亂碼問題的解決方法

    這篇文章主要為大家詳細介紹了java spark文件讀取亂碼問題的相關解決方法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-10-10
  • java如何實現(xiàn)socket連接方法封裝

    java如何實現(xiàn)socket連接方法封裝

    這篇文章主要介紹了java實現(xiàn)socket連接方法封裝教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringMvc3+extjs4實現(xiàn)上傳與下載功能

    SpringMvc3+extjs4實現(xiàn)上傳與下載功能

    這篇文章主要為大家詳細介紹了SpringMvc3+extjs4實現(xiàn)上傳與下載功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Java java.sql.Timestamp時間戳案例詳解

    Java java.sql.Timestamp時間戳案例詳解

    這篇文章主要介紹了Java java.sql.Timestamp時間戳案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08
  • SpringBoot集成WebSocket長連接實際應用詳解

    SpringBoot集成WebSocket長連接實際應用詳解

    這篇文章主要介紹了SpringBoot集成WebSocket長連接實際應用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06

最新評論