当前位置 主页 > 服务器问题 > 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());
      }