如何告诉Maven忽略SSL错误(并信任所有证书)?

175

我经常需要运行 "mvn" 命令:

mvn -f pom.xml clean install -Dmaven.test.skip=false --settings /Users/myhome/settings.xml -X -Djavax.net.ssl.trustStore=/Users/myhome/truststore.jks -Djavax.net.ssl.trustStoreType=JKS -Djavax.net.ssl.trustStorePassword=dummy -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol -U

由于我需要与各种其他领域进行集成,因此每次都需要将他们的证书添加到我的truststore.jks中,以防止SSL握手错误。

是否有办法配置mvn以忽略SSL错误。

8个回答

383

你可以通过添加以下命令行参数来禁用SSL证书检查:

  • -Dmaven.wagon.http.ssl.insecure=true - 启用对用户生成的证书进行宽松的SSL检查。
  • -Dmaven.wagon.http.ssl.allowall=true - 启用将服务器的X.509证书与主机名进行匹配。如果禁用,则将使用类似浏览器的检查。
  • -Dmaven.wagon.http.ssl.ignore.validity.dates=true - 忽略证书日期的问题。
  • -Dmaven.resolver.transport=wagon - 在Maven 3.9.0及更高版本中,默认切换到使用Apache HttpClient 4。您需要使用此选项将其切换回wagon,以使上述标志生效。

官方文档:http://maven.apache.org/wagon/wagon-providers/wagon-http/

以下是一个简单的一行命令,方便复制粘贴:

-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true
Ajay Gautam建议您还可以将上述内容添加到~/.mavenrc文件中,以免每次在命令行中都需要指定。
$ cat ~/.mavenrc 
MAVEN_OPTS="-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true"

2
我有一个 Spring Boot/Maven 项目,并且使用 maven-jaxb2-plugin 生成 WebService 的源代码。但是证书已经过期了。因此,我尝试在 Eclipse 中使用上述参数运行 Maven 生成源代码。但是我仍然遇到 "时间戳检查失败" 的错误。请问在 Eclipse/Maven 环境中如何解决这个问题? - Mukun
36
这个答案对我没用。我正在使用maven 3.5.0,我收到的异常是PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target。wagon可以与Maven 3.5.0一起使用吗? - thlim
3
安装Java密码扩展(JCE)无限制强度版(适用于JDK7 | 适用于JDK8)解决了我在maven 3.5.0上的问题。 - skadya
2
请注意,maven.wagon.http.ssl.ignore.validity.dates=true 只有在您同时设置 maven.wagon.http.ssl.insecure=true 时才起作用。 - Björn Zurmaar
9
如果您使用的是Maven 3.9.0,您还需要添加以下内容:-Dmaven.resolver.transport=wagon - ArtOfWarfare
显示剩余9条评论

50

对我而言,一个可行的替代方案是告诉Maven在使用Maven中心库时使用http:而不是https:,方法是在settings.xml中添加以下内容:

<settings>
   .
   .
   .
  <mirrors>
    <mirror>
        <id>central-no-ssl</id>
        <name>Central without ssl</name>
        <url>http://repo.maven.apache.org/maven2</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
   .
   .
   .
</settings>

当然,您的情况可能会有所不同。


2
谢谢,这个方法有效!请注意一旦SSL生效后要恢复原状,因为这可能会导致用户添加非标准仓库,其中可能包含任何东西。 - asgs
11
自2020年1月起,由于Maven仓库禁用了HTTP连接,因此此方法不再起作用。 - Justin
2
自2020年1月起,将会收到以下错误:"ReasonPhrase: HTTPS Required",因为Maven仓库已禁用Http连接。 - Justin
这对我有用。但是,我不得不在URL中添加https。不确定为什么。 - Michael
2
这个无法工作,因为自2020年1月起,未加密的http连接被501阻止。 它影响的地方不仅仅是Central。 JCenter、Spring和Gradle存储库也需要使用https。详情请阅读:Jan 2020, https://www.alphabot.com/security/blog/2020/java/Your-Java-builds-might-break-starting-January-13th.html - Matthew Dowell

47

创建一个名为 ${USER_HOME}/.mvn 的文件夹,并在其中放置一个名为 maven.config 的文件。

文件内容应为:

-Dmaven.wagon.http.ssl.insecure=true
-Dmaven.wagon.http.ssl.allowall=true
-Dmaven.wagon.http.ssl.ignore.validity.dates=true

希望这可以帮助到你。


2
我在命令行中直接使用这些参数成功了,但将它们添加到文件中后仍然失败了。 - Justin
5
这实际上应该在项目级别进行配置。 http://maven.apache.org/configure.html#mvn-folder - Adam Ostrožlík
1
运行得非常好。我能够在控制台下完成它,但不能在IDE中完成。 - Davut Gürbüz
我在我的IntelliJ IDE上通过在这里添加它们使其工作:设置 > 构建、执行、部署 > 构建工具 > Maven > 导入 > VM选项。 - undefined

3
如果由于任何原因 maven.config 无法工作:
尝试将内容设置为环境变量。
例如:
MAVEN_OPTS="-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true Dmaven.wagon.http.ssl.ignore.validity.dates=true -Dhttps.protocols=TLSv1.2"

设置环境变量后,您可以轻松运行mvn命令。

对于简短的测试,您可以为会话设置环境变量

Powershell:

$env:MAVEN_OPTS="-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true -Dhttps.protocols=TLSv1.2"

Bash:

export MAVEN_OPTS="-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true -Dhttps.protocols=TLSv1.2"

CMD:

set MAVEN_OPTS="-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true -Dhttps.protocols=TLSv1.2"

我可以确认,这些标志是2023年4月底我唯一起作用的标志。谢谢。 - sueszli

1
如果您想将所有相同的 maven.wagon.http.ssl. 设置放入 ~/.m2/settings.xml 而不是 ~/.mavenrc,则需要在文件中添加以下内容:
<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <profiles>
    <profile>
      <id>definedInM2SettingsXML</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <maven.wagon.http.ssl.insecure>true</maven.wagon.http.ssl.insecure>
        <maven.wagon.http.ssl.allowall>true</maven.wagon.http.ssl.allowall>
        <maven.wagon.http.ssl.ignore.validity.dates>true</maven.wagon.http.ssl.ignore.validity.dates>
      </properties>
    </profile>
  </profiles>
</settings>

1
我发现最新的jdk16会失败SSL证书,所以我不得不使用-Dmaven.wagon.http.ssl.ignore.validity.dates=true来解决问题;切换到jdk11(LTS),所有问题都解决了。
同时也测试了jdk1.8,它也可以在没有任何参数的情况下工作;但是jdk1.8处于无更新模式,最好升级到LTS版本的jdk,而不是最新的jdk16。

0

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接