當前位置 主頁 > 服務器問題 > Linux/apache問題 > 最大化 縮小

    Spring Security實現驗證碼登錄功能

    欄目:Linux/apache問題 時間:2020-01-19 00:44

    這篇文章主要介紹了Spring Security實現驗證碼登錄功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

    在spring security實現登錄注銷功能的基礎上進行開發。

    1、添加生成驗證碼的控制器。

    (1)、生成驗證碼

    /**
       * 引入 Security 配置屬性類
       */
      @Autowired
      private SecurityProperties securityProperties;
    
    
      @Override
      public ImageCode createCode(HttpServletRequest request ) {
        //如果請求中有 width 參數,則用請求中的,否則用 配置屬性中的
        int width = ServletRequestUtils.getIntParameter(request,"width",securityProperties.getWidth());
        //高度(寬度)
        int height = ServletRequestUtils.getIntParameter(request,"height",securityProperties.getHeight());
        //圖片驗證碼字符個數
        int length = securityProperties.getLength();
        //過期時間
        int expireIn = securityProperties.getExpireIn();
    
        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 < length; 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, expireIn);
      }
    
      /**
       * 生成隨機背景條紋
       */
      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);
      }

    (2)、驗證碼控制器

    public static final String SESSION_KEY = "SESSION_KEY_IMAGE_CODE";
    
      @Autowired
      private ValidateCodeGenerator imageCodeGenerator;
    
      /**
       * Session 對象
       */
      private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();
    
      @GetMapping("/code/image")
      public void createCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ImageCode imageCode = imageCodeGenerator.createCode(request);
        //將隨機數 放到Session中
        sessionStrategy.setAttribute(new ServletWebRequest(request),SESSION_KEY,imageCode);
        request.getSession().setAttribute(SESSION_KEY,imageCode);
        //寫給response 響應
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        ImageIO.write(imageCode.getImage(),"JPEG",response.getOutputStream());
      }
    
    下一篇:沒有了
777亚洲人成视频免费视频