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

SpringBoot整合通用ClamAV文件扫描病毒

 前提:在可运行的SpringBoot的项目内引用以下JAR包

 整个工具的代码都在Gitee或者Github地址内

gitee:solomon-parent: 这个项目主要是总结了工作上遇到的问题以及学习一些框架用于整合例如:rabbitMq、reids、Mqtt、S3协议的文件服务器、mongodb

github:GitHub - ZeroNing/solomon-parent: 这个项目主要是总结了工作上遇到的问题以及学习一些框架用于整合例如:rabbitMq、reids、Mqtt、S3协议的文件服务器、mongodb

需要引入的JAR包(版本根据自身要求使用,本教程用的版本均为最新)

    <dependency><groupId>xyz.capybara</groupId><artifactId>clamav-client</artifactId></dependency>

1.Docker Compose部署ClamAV

version: '3.8'
services:clamav:image: clamav/clamav:latestcontainer_name: clamavvolumes:- ./data:/var/lib/clamav  # 病毒库持久化- ./scan:/scandir         # 待扫描目录ports:- "3310:3310"
#    environment:
#      - CLAMAV_NO_FRESHCLAMD=false        # 开启自动更新restart: unless-stopped

需要创建 data以及scan文件夹

2.新增ClamAV配置类

@ConfigurationProperties("clamav")
public class ClamAvProperties {private String host;private Integer port;private boolean enabled = false;private Platform platform = Platform.UNIX;public Platform getPlatform() {return platform;}public void setPlatform(Platform platform) {this.platform = platform;}public boolean getEnabled() {return enabled;}public void setEnabled(boolean enabled) {this.enabled = enabled;}public String getHost() {return host;}public void setHost(String host) {this.host = host;}public Integer getPort() {return port;}public void setPort(Integer port) {this.port = port;}
}
@Configuration
@EnableConfigurationProperties(value = {ClamAvProperties.class})
public class ClamAvConfig {private final ClamAvProperties clamAVProperties;public ClamAvConfig(ClamAvProperties clamAVProperties) {this.clamAVProperties = clamAVProperties;}@Beanpublic ClamAvUtils clamAvUtils() {ClamavClient client = null;if(clamAVProperties.getEnabled()){client = new ClamavClient(clamAVProperties.getHost(), clamAVProperties.getPort(), clamAVProperties.getPlatform());}return new ClamAvUtils(client,clamAVProperties);}
}

3.新增ClamAV扫描病毒工具类

public class ClamAvUtils {private final Logger logger = LoggerUtils.logger(ClamAvUtils.class);private final ClamavClient client;private final ClamAvProperties properties;public ClamAvUtils(ClamavClient client, ClamAvProperties properties) {this.client = client;this.properties = properties;}/*** 扫描文件是否有病毒* @param inputStream 文件流* @return true 有病毒 false 没有病毒* @throws BaseException*/public boolean scanFile(InputStream inputStream) throws BaseException {if(!properties.getEnabled()){return false;}if(ValidateUtils.isEmpty(inputStream)){throw new IllegalArgumentException("Input stream is empty");}ScanResult scanResult = client.scan(inputStream);if(scanResult instanceof ScanResult.OK) {return false;}if(scanResult instanceof ScanResult.VirusFound) {Map<String, Collection<String>> foundViruses = ((ScanResult.VirusFound) scanResult).getFoundViruses();logger.info(foundViruses.toString());return true;}throw new BaseException("ERROR_CODE_SCAN_FILE_ERROR");}/*** 扫描文件是否有病毒* @param inputStream 文件流* @param errorCode 有病毒异常编码* @throws BaseException*/public void scanFile(InputStream inputStream,String errorCode) throws BaseException {if(!scanFile(inputStream)){throw new BaseException(errorCode);}}
}


文章转载自:

http://IT0SkSvE.fwnqq.cn
http://ZmaUnUcS.fwnqq.cn
http://YxT8KMdW.fwnqq.cn
http://uMuDEliy.fwnqq.cn
http://nNdBrrZM.fwnqq.cn
http://CxMx9DFw.fwnqq.cn
http://AuikK3zD.fwnqq.cn
http://E7jKlQJj.fwnqq.cn
http://qTirr2JI.fwnqq.cn
http://Hgi6RMAu.fwnqq.cn
http://QlTYaq3L.fwnqq.cn
http://lfEDNWAd.fwnqq.cn
http://q8Jiwrzb.fwnqq.cn
http://ja2mjax4.fwnqq.cn
http://a1vRj1Q3.fwnqq.cn
http://UPH9xYvQ.fwnqq.cn
http://mAaIxMRs.fwnqq.cn
http://WUQTEvTB.fwnqq.cn
http://u9ONtU7I.fwnqq.cn
http://jgjS7iW0.fwnqq.cn
http://rfL9sdrC.fwnqq.cn
http://O1uXt9dM.fwnqq.cn
http://k9I7nQfF.fwnqq.cn
http://zxDPNbGr.fwnqq.cn
http://GUE5Wmm8.fwnqq.cn
http://bUedlwxJ.fwnqq.cn
http://wNt2nA0N.fwnqq.cn
http://9a3m2RFT.fwnqq.cn
http://gHa03HTb.fwnqq.cn
http://whpeQfif.fwnqq.cn
http://www.dtcms.com/a/375305.html

相关文章:

  • 提权分析报告 —— 基于DriftingBlues: 4靶场
  • 设计模式-原则概述
  • LAMPSecurity: CTF8靶场渗透
  • python网络爬取个人学习指南-(五)
  • CSS 基础概念
  • 在企业内部分发 iOS App 时如何生成并使用 manifest.plist
  • AJAX入门-AJAX 概念和 axios 使用
  • 框架-MyBatis|Plus-1
  • Spring Boot 2.7 启动流程详解
  • springboot框架使用websocket实现一个聊天室的细节
  • Kubernetes集群部署Jenkins指南
  • 027、全球数据库市场深度分析:技术革命下的产业格局重塑
  • 贪心算法与动态规划:数学原理、实现与优化
  • Oracle APEX 利用卡片实现翻转(方法二)
  • 记一次 electron 添加 检测 终端编码,解决终端打印中文乱码问题
  • 从生活照料到精神关怀,七彩喜打造全场景养老服务体系
  • 2025-09-08升级问题记录: 升级SDK从Android11到Android12
  • BizDevOps 是什么?如何建设企业 BizDevOps 体系
  • 一、ARM异常等级及切换
  • 【项目复现】MOOSE-Chem 用于重新发现未见化学科学假说的大型语言模型
  • mybatis plus 使用wrapper输出SQL
  • PgSQL中优化术语HOT详解
  • Python 绘制 2025年 9~11月 P/1999 RO28 (LONEOS) 彗星路径
  • Spring Cloud Stream深度实战:发布订阅模式解决微服务通信难题
  • 【菜狗每日记录】深度轨迹聚类算法、GRU门控神经网络—20250909
  • OpenCV 实战:多角度模板匹配实现图像目标精准定位
  • C#/.NET/.NET Core技术前沿周刊 | 第 53 期(2025年9.1-9.7)
  • 基于Java+Vue开发的家政服务系统源码适配H5小程序APP
  • 使用Flask实现接口回调地址
  • Java线程中的sleep、wait和block:区别与联系详解