| | |
| | | /** |
| | | * 文件白名单 |
| | | */ |
| | | @Value("${app.upload.security.allowed-extensions:jpg,jpeg,png,pdf}") |
| | | @Value("${app.upload.security.allowed-extensions:xls,xlsx,jar,doc,docx,jpg,jpeg,png,pdf}") |
| | | private String allowedExtensionsConfig; |
| | | |
| | | /** |
| | |
| | | |
| | | private boolean contentSecurityValidation(MultipartFile file, UploadValidationResult result) { |
| | | // 内容类型验证 |
| | | if (validateContentType && !validateContentType(file)) { |
| | | result.setMessage("文件内容类型不匹配"); |
| | | return false; |
| | | if (validateContentType) { //配置了校验位true |
| | | //后缀不在白名单中,并且文件内容与类型不匹配直接返回 |
| | | String finalExtension = getFinalExtension(file.getOriginalFilename().toLowerCase()); |
| | | if(!allowedExtensions.contains(finalExtension) && !validateContentType(file)){ |
| | | result.setMessage("文件内容类型不匹配[后缀:"+finalExtension+";ContentType:"+file.getContentType()+"]"); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | // 文件头验证 |
| | |
| | | if (declaredType == null) { |
| | | return true; // 没有声明类型,放过 |
| | | } |
| | | |
| | | log.info("===========================ContentType: " + declaredType); |
| | | // 简单的类型匹配检查 |
| | | String finalExtension = getFinalExtension(file.getOriginalFilename()).toLowerCase(); |
| | | log.info("===========================finalExtension: " + finalExtension); |
| | | |
| | | return isContentTypeConsistent(declaredType, finalExtension); |
| | | } catch (Exception e) { |
| | | log.error("内容类型验证失败", e); |