Maven 3.8如何将私有Nexus仓库加入受信白名单
Maven 3.8+ 私有HTTP仓库被拦截问题处理
问题场景
- Maven 从3.8版本开始新增MITM攻击防护机制,默认拦截所有非受信的HTTP协议仓库连接
- 原配置在Maven 3.6.3下可正常使用的公司内部Sonatype Nexus私有仓库,升级到3.8+版本后下载依赖时会出现连接挂起、无限等待无响应的情况
- 私有仓库已按常规配置写入
settings.xml,包括仓库地址、对应访问账号密码
问题根源
Maven的拦截逻辑来自内置的默认全局阻塞镜像配置,该配置的匹配规则为所有外网HTTP协议的仓库,命中规则的请求会被导向一个不存在的空地址,直接导致连接无响应。该规则是Maven为了避免未加密传输导致的依赖投毒、中间人攻击风险设置的强制安全策略,不会自动识别企业内部的受信私有仓库。
具体配置修改
针对当前已有的settings.xml配置(私有仓库id为my-nexus),只需要在现有配置中补充mirrors节点,将该私有仓库加入受信列表,绕过默认拦截规则即可。
修改后的完整配置参考如下:
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- 新增:配置受信仓库的镜像绕过规则 --> <mirrors> <mirror> <id>trusted-internal-nexus</id> <!-- 排除id为my-nexus的私有仓库,剩余仓库使用中央仓库地址匹配 --> <mirrorOf>!my-nexus,*</mirrorOf> <url>https://repo.maven.apache.org/maven2/</url> </mirror> </mirrors> <profiles> <profile> <id>first-profile</id> <repositories> <repository> <id>my-nexus</id> <url>http://nexus.mycompany.com:8081/repository/dev/</url> </repository> </repositories> </profile> </profiles> <servers> <server> <id>my-nexus</id> <username>username</username> <password>password</password> </server> </servers> <activeProfiles> <activeProfile>first-profile</activeProfile> </activeProfiles> </settings>
注意事项
- 不需要直接修改Maven安装目录下
conf/settings.xml中的默认阻塞配置,避免失去默认的安全防护能力 - 配置中
mirrorOf的感叹号为排除语法,只要和已配置的私有仓库id保持一致,即可绕过默认拦截 - 修改完成后执行带
-U参数的Maven命令强制刷新配置,验证依赖是否可正常拉取 - 若条件允许,可将内部Nexus仓库配置为HTTPS协议访问,从根源上规避HTTP仓库的强制拦截规则
内容的提问来源于stack exchange,提问作者Christian G. Faraone
相关产品推荐
相关产品推荐

