解决 Http Maven 私仓的依赖包无法下载,提示被 maven-default-http-blocker 拦截

一、环境

  1. 用到了HTTP接口的Maven私仓
  2. maven版本是3.8.1级以上
  3. pom文件已经配置了repository

二、错误信息

才尝试更新pom文件,重新下载依赖,或者,mvn clean compile重新编译的时候,会提示类似下面的错误信息:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.477 s
[INFO] Finished at: 2023-03-29T09:02:46+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project app-demo: Could not resolve dependencies for project markvivv:app-demo:jar:3.0.0.RELEASE: The following artifacts could not be resolved: simpleteam:simpleteam:jar:1.0.0 (present, but unavailable): simpleteam:simpleteam:jar:1.0.0 was not found in http://192.168.0.18:8081/ during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of maven-default-http-blocker has elapsed or updates are forced -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException

另一种可能的错误提示: 

simpleteam:simpleteam:jar:1.0.0 was not found in http://192.168.0.18:8081/ during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of maven-default-http-blocker has elapsed or updates are forced

尝试使用 -U 标记(强制更新快照)运行 Maven 导入

 错误的提示都指向 maven-default-http-blocker

三、错误原因

这是由于Maven官方为了解决CVE-2021-26291安全问题,在3.8.1版本开始发布这个特性,默认禁止从不安全的仓库下载依赖。

maven官方对CVE-2021-26291说明:

  1. 由于自定义资源库使用HTTP,可能出现中间人攻击的情况。虽然越来越多的存储库使用HTTPS,但这并不都是这样。这意味着Maven Central包含有通过HTTP引用URL的自定义仓库的POM。这使得通过这类资源库的下载成为MITM攻击(中间人攻击)的目标。同时,开发者可能没有意识到有些下载使用的是不安全的URL。由于上传到Maven Central的POM是不可更改的,因此需要对Maven进行修改。为了解决这个问题,我们用<blocked>参数扩展了镜像配置,并增加了一个新的external:http:*镜像选择器(和现有的external:*一样),意思是 "任何使用HTTP的外部URL"。
  2. 我们决定在默认情况下阻止这样的外部HTTP资源库:这是通过在conf/settings.xml中提供一个阻止不安全的HTTP外部URL的镜像来实现的。
  3. 由于自定义资源库使用废弃的域名,可能出现域名劫持的情况。Sonatype已经分析了哪些域名被遗弃,并对这些域名进行了认领。
  4. 可能通过重定向到自定义存储库来劫持下载。这个是最难分析和解释的。简而言之就是:你是安全的,依赖项只在其上下文中从存储库下载。所以有两个主要问题:什么是上下文,什么是顺序?顺序在版本库顺序页上有描述。
    1. 第一组软件库是在 settings.xml 中定义的(包括用户和全局)。
    2. 第二组仓库是基于继承性的,最终超级POM包含Maven Central的URL。
    3. 第三组是最复杂的一组,但对理解语境很重要:从依赖路径到工件的有效POM的资料库。因此,如果一个依赖关系是由另一个依赖关系或Maven项目定义的,也会包括它们的仓库。说到底,这不是一个bug,而是一个设计特性。

所以一切为了安全,maven3.8.1版本开始加上了限制。

四、解决办法

4.1. 删除 maven-default-http-blocker 配置

文件位置在 ${maven.home}/conf/settings.xml 或 ${user.home}/.m2/settings.xml,具体需要删除的内容如下:

    <mirror>
      <id>maven-default-http-blocker</id>
      <mirrorOf>external:http:*</mirrorOf>
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
    </mirror>

但是,这样子删除之后违反了maven添加这样一个特性的初衷,引入了风险项。并且这种设置会要求使用到私仓的客户端都进行修改,如果推广培训不好,每个人都会需要查一遍这个问题,然后才知道要做对应的配置,下面安排第二种方法。

4.2. 创建项目个性化的maven配置,随代码一起提交到代码仓库

在项目的根目录添加一个.mvn的目录,并配置maven.config和local-settings.xml两个文件。

maven 3.8.1以上,maven 3.9以下maven.config文件内容如下:

--settings ./.mvn/local-settings.xml

maven 3.9及以上版本的maven.config文件内容如下,注意配置项要放到新的一行,否则配置不生效:

--settings
./.mvn/local-settings.xml

local-settings.xml配置文件内容,注意mirrorOf的ID值需要和pom.xml中的私仓ID一致:

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">
    <mirrors>
        <mirror>
            <id>my-repository-http-unblocker</id>
            <mirrorOf>markvivv-repos</mirrorOf>
            <name></name>
            <url>http://192.168.7.18:8081/repository/maven-releases</url>
        </mirror>
    </mirrors>

pom.xml中私仓的示例配置,注意ID和.mvn/local-settings.xml中的mirrorOf配置的ID一致:

    <repositories>
        <repository>
            <id>ovit-repos</id>
            <name>markvivv-repos</name>
            <url>http://192.168.7.18:8081/repository/maven-releases</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

把.mvn目录提交到代码仓库,这样项目的其他开发人员下载到源码的时候,就包含这个配置,不需要再做额外配置。

猜你喜欢

转载自blog.csdn.net/xieshaohu/article/details/129829610
今日推荐