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);}}
}