当前位置: 首页 > news >正文

Spring Boot 校验用户上传的图片文件

图片上传是现代应用中非常常见的一种功能,也是风险比较高的一个地方。恶意用户可能会上传一些病毒、木马。这些东西不仅严重威胁服务器的安全还浪费了带宽,磁盘等资源。所以,在图片上传的接口中,一定要对用户上传的文件进行严格的校验

本文介绍了 2 种对图片文件进行验证的方法可供你参考。

一、文件后缀校验

通过文件后缀(也就是文件扩展名,通常用于表示文件的类型),进行文件类型校验这是最常见的做法。

图片文件的后缀类型有很多,常见的只有:jpgjpeggifpngwebp。我们可以在配置或者代码中定义一个“允许上传的图片后缀”集合,用于校验用户上传的图片文件。

package cn.springdoc.demo.web.controller;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/upload")
public class UploadController {

    // 允许上传的图片类型的后缀集合
    static final Set<String> imageSuffix = Set.of("jpg", "jpeg", "gif", "png", "webp");


    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<String> upload (@RequestParam("file") MultipartFile file ) throws IllegalStateException, IOException{
        
        // 文件的原始名称
        String fileName = file.getOriginalFilename();
        if (fileName == null) {
            return ResponseEntity.badRequest().body("文件名称不能为空");
        }
        
        // 解析出文件后缀
        int index = fileName.lastIndexOf(".");
        if (index == -1) {
            return ResponseEntity.badRequest().body("文件后缀不能为空");
        }
        
        String suffix = fileName.substring(index + 1);
        
        if (!imageSuffix.contains(suffix.trim().toLowerCase())) {
            return ResponseEntity.badRequest().body("非法的文件类型");
        }
        
        // IO 到程序运行目录下的 public 目录,这是默认的静态资源目录
        
        Path dir = Paths.get(System.getProperty("user.dir"), "public");
        if (!Files.isDirectory(dir)) {
            // 创建目录
            Files.createDirectories(dir);
        }
        
        file.transferTo(dir.resolve(fileName));

        // 返回相对访问路径
        return ResponseEntity.ok("/" + fileName);
    }
}

如上,代码很简单。先是获取客户端文件的名称,再从名称获取到文件的后缀。确定是合法文件后再 IO 到本地磁盘。

二、使用 ImageIO 校验

由于文件的后缀是可编辑的,恶意用户可以把一个 exe 文件的后缀改为 jpg 再上传到服务器。于是这种情况下,上面的这种校验方式就会失效,恶意文件会被 IO 到磁盘。

在基于上面的方法进行校验后,我们可以先把文件 IO 到临时目录,再使用 ImageIO 类去加载图片文件,如果 Images 加载的文件不是图片,则会返回 null

package cn.springdoc.demo.web.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/upload")
public class UploadController {

    // 允许上传的图片类型的后缀集合
    static final Set<String> imageSuffix = Set.of("jpg", "jpeg", "gif", "png", "webp");

    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file)
            throws IllegalStateException, IOException {

        // 文件的原始名称
        String fileName = file.getOriginalFilename();
        if (fileName == null) {
            return ResponseEntity.badRequest().body("文件名称不能为空");
        }

        // 解析出文件后缀
        int index = fileName.lastIndexOf(".");
        if (index == -1) {
            return ResponseEntity.badRequest().body("文件后缀不能为空");
        }

        String suffix = fileName.substring(index + 1);

        if (!imageSuffix.contains(suffix.trim().toLowerCase())) {
            return ResponseEntity.badRequest().body("非法的文件类型");
        }

        // 获取系统中的临时目录
        Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));

        // 临时文件使用 UUID 随机命名
        Path tempFile = tempDir.resolve(Paths.get(UUID.randomUUID().toString()));

        // copy 到临时文件
        file.transferTo(tempFile);

        try {
            // 使用 ImageIO 读取文件
            if (ImageIO.read(tempFile.toFile()) == null) {
                return ResponseEntity.badRequest().body("非法的文件类型");
            }
            
            // 至此,这的确是一个图片资源文件
            // IO 到运行目录下的 public 目录
            Path dir = Paths.get(System.getProperty("user.dir"), "public");
            if (!Files.isDirectory(dir)) {
                // 创建目录
                Files.createDirectories(dir);
            }

            Files.copy(tempFile, dir.resolve(fileName));
            
            // 返回相对访问路径
            return ResponseEntity.ok("/" + fileName);
            
        } finally {
            // 始终删除临时文件
            Files.delete(tempFile);
        }
    }
}

这种方式更为严格,不但要校验文件后缀还要校验文件内容。弊端也显而易见,会耗费更多的资源!

1、ImageIO.read 方法

最后说一下 ImageIO.read 方法,它会从系统中已注册的 ImageReader 中选择一个 Reader 对图片进行解码,如果没有 Reader 能够解码文件,则返回 null。也就是说,如果上传的图片类型,在系统中没有对应的 ImageReader 也会被当做是“非法文件”。

例如:webp 类型的图片文件,ImageIO.read 就读取不了,因为 JDK 没有预置读取 webp 图片的 ImageReader

要解决这个问题,可以添加一个 webp-imageio 依赖。

<!-- https://mvnrepository.com/artifact/org.sejda.imageio/webp-imageio -->
<dependency>
    <groupId>org.sejda.imageio</groupId>
    <artifactId>webp-imageio</artifactId>
    <version>0.1.6</version>
</dependency>

webp-imageio 库提供了 webp 图片的 ImageReader 实现,并以 SPI 的形式注册到了系统中,不要写其他任何代码就可以成功读取。

相关文章:

  • 【Qt】撤销/恢复的快捷键
  • Git基本概念和使用方式
  • 【MySQL系列】第二章 · SQL(上)
  • Rd-03D串口协议
  • 【Opencv】cv::dnn::NMSBoxes()函数详解
  • 华为云Ascend310服务器使用
  • JVM在线分析-监控工具(jps, jstat, jstatd)
  • 智慧城市建设解决方案分享【完整】
  • 为 Ubuntu 虚拟机构建 SSH 服务器
  • 2023年11月在线IDE流行度最新排名
  • 基于springboot的在线文档管理系统
  • 基于springboot实现桥牌计分管理系统项目【项目源码】计算机毕业设计
  • Android Studio里的C/C++返回: ld: error: undefined symbol
  • 【Go入门】面向对象
  • Vatee万腾科技决策力的未来展望:开创数字化创新的新高度
  • 音视频基础知识
  • Linux之基础开发工具gdb调试器的使用(三)
  • 数据存储_PGSQL
  • 【毕业论文】基于python爬虫对豆瓣影评分析系统的设计与实现
  • 【第七章】软件设计师 之 程序设计语言与语言程序处理程序基础
  • 国际金价下跌,中概股多数上涨,穆迪下调美国主权信用评级
  • 再现五千多年前“古国时代”:凌家滩遗址博物馆今开馆
  • 农行再回应客户办理业务期间离世:亲属连续三次输错密码,理解亲属悲痛,将协助做好善后
  • 李公明︱一周书记:当前科学观中的盲点、危机与……人类命运
  • 中科飞测将投资超10亿元,在上海张江成立第二总部
  • 横跨万里穿越百年,《受到召唤·敦煌》中张艺兴一人分饰两角