好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringBoot中验证用户上传的图片资源的方法

允许用户上传图片资源(头像,发帖)是APP常见的需求,特别需要把用户的资源IO到磁盘情况下,需要防止坏人提交一些非法的文件,例如木马,webshell,可执行程序等等。这类非法文件不仅会导致客户端图片资源显示失败,而且还会给服务器带来安全问题。

通过文件后缀判断文件的合法性

这种方式比较常见,也很简单,是目前大多数APP选择的做法。

?

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

public Object upload ( @RequestParam ( "file" ) MultipartFile multipartFile) throws IllegalStateException, IOException {

    

     // 原始文件名称

     String fileName = multipartFile.getOriginalFilename();

    

     // 解析到文件后缀,判断是否合法

     int index = fileName.lastIndexOf( "." );

     String suffix = null ;

     if (index == - 1 || (suffix = fileName.substring(index + 1 )).isEmpty()) {

         return "文件后缀不能为空" ;

     }

    

     // 允许上传的文件后缀列表

     Set<String> allowSuffix = new HashSet<>(Arrays.asList( "jpg" , "jpeg" , "png" , "gif" ));

     if (!allowSuffix.contains(suffix.toLowerCase())) {

         return "非法的文件,不允许的文件类型:" + suffix;

     }

    

     // 序列化到磁盘中的文件上传目录, /upload

     // FileCopyUtils.copy 方法会自动关闭流资源

     FileCopyUtils.copy(multipartFile.getInputStream(), Files.newOutputStream(Paths.get( "D://upload" , fileName), StandardOpenOption.CREATE_NEW));

    

     // 返回相对访问路径,文件名极有可能带中文或者空格等字符,进行uri编码

     return   "/" + UriUtils.encode(fileName, StandardCharsets.UTF_8);

}

使用 ImageIO 判断是否是图片

这个方法就比较严格了,在判断后缀的基础上,使用Java的 ImageIO 类去加载图片,尝试读取其宽高信息,如果不是合法的图片资源。则无法读取到这两个数据。就算是把非法文件修改了后缀,也可以检测出来。

?

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

public Object upload ( @RequestParam ( "file" ) MultipartFile multipartFile) throws IllegalStateException, IOException {

    

     // 原始文件名称

     String fileName = multipartFile.getOriginalFilename();

    

     // 解析到文件后缀

     int index = fileName.lastIndexOf( "." );

     String suffix = null ;

     if (index == - 1 || (suffix = fileName.substring(index + 1 )).isEmpty()) {

         return "文件后缀不能为空" ;

     }

    

     // 允许上传的文件后缀列表

     Set<String> allowSuffix = new HashSet<>(Arrays.asList( "jpg" , "jpeg" , "png" , "gif" ));

     if (!allowSuffix.contains(suffix.toLowerCase())) {

         return "非法的文件,不允许的文件类型:" + suffix;

     }

    

     // 临时文件

     File tempFile = new File(System.getProperty( "java.io.tmpdir" ), fileName);

    

     try {

         // 先把文件序列化到临时目录

         multipartFile.transferTo(tempFile);

         try {

             // 尝试IO文件,判断文件的合法性

             BufferedImage  bufferedImage = ImageIO.read(tempFile);

             bufferedImage.getWidth();

             bufferedImage.getHeight();

         } catch (Exception e) {

             // IO异常,不是合法的图片文件,返回异常信息

             return "文件不是图片文件" ;

         }

         // 复制到到上传目录

         FileCopyUtils.copy( new FileInputStream(tempFile), Files.newOutputStream(Paths.get( "D://upload" , fileName), StandardOpenOption.CREATE_NEW));

         // 返回相对访问路径

         return   "/" + UriUtils.encode(fileName, StandardCharsets.UTF_8);

     } finally {

         // 响应客户端后,始终删除临时文件

         tempFile.delete();

     }

}

总结

使用 ImageIo 的方式更为保险,但是需要多几次IO操作。比较消耗性能。而且今天APP大都是用云存储服务,类似于阿里云的OSS。直接就把客户端上传的文件PUT到了云端,才不管用户上传的图片是不是真正的图片,非法上传,最多导致客户端不能显示而已,但是威胁不了服务器的安全。

原文:https://springboot.io/t/topic/2231

到此这篇关于在SpringBoot中验证用户上传的图片资源的文章就介绍到这了,更多相关SpringBoot验证用户上传的图片资源内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://www.cnblogs.com/kevinblandy/p/13308385.html

查看更多关于SpringBoot中验证用户上传的图片资源的方法的详细内容...

  阅读:24次