9

解决 Maven 的 unable to find valid certification path to requested target 错误

 3 years ago
source link: https://www.boris1993.com/tools/maven/maven-unable-to-find-valid-certification-path-to-requested-target.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

解决 Maven 的 unable to find valid certification path to requested target 错误

2020-03-30 2020-12-10工具Maven

1029 0

今天我在从公司的 Maven 私库上下载依赖时,Maven 报出了 unable to find valid certification path to requested target 的问题,经查询是因为没有将私库使用的自签名证书导入到 Java 的 keystore。这里写一下我的两个解决方案,方便发生了同样问题的人参考。

让 Maven 忽略证书错误

因为在默认情况下,自签名证书是不受信任的,那么我们只要让 Maven 忽略证书错误,就可以解决这个问题了。

我们可以在.bashrc.zshrc(或者其他位置,看你用哪个 shell 了)里面加入如下语句:

1
export MAVEN_OPTS='-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Djsse.enableSNIExtension=false'

或者,也可以在 ~/.mavenrc 中配置这个参数:

1
MAVEN_OPTS="-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Djsse.enableSNIExtension=false"

导入证书到 keystore

首先使用这个命令得到网站上面的证书:

1
2
# 注意将mavenrepository.mycompany.com替换成实际的地址
openssl s_client -showcerts -connect mavenrepository.mycompany.com:443

在输出中可以看到类似这样的证书信息:

1
2
3
s:/C=US/ST=North Carolina/L=Raleigh/O=Red Hat, Inc./OU=Red Hat Network/CN=Entitlement Master CA/[email protected]

i:/C=US/ST=North Carolina/L=Raleigh/O=Red Hat, Inc./OU=Red Hat Network/CN=Entitlement Master CA/[email protected]

我们要在这里面寻找 issuer(i:)subject(s:) 相同的证书,这个就是我们要导出的根证书。找到之后,保存证书的哈希(即以 —–BEGIN CERTIFICATE—–开始,以 —–END CERTIFICATE—–结束的这部分内容)到一个文件中,比如 root.crt

接下来,我们需要使用 keytool 把这个证书导入到 Java 的 keystore 中,命令如下:

1
2
3
4
5
6
7
8
9
10
sudo keytool \
-importcert \
# keystore文件位置需要修改成实际的位置
-keystore /usr/local/java/jdk1.8.0_60/jre/lib/security/cacerts \
# keystore密码需要修改成实际的密码,默认密码是changeit
-storepass changeit \
# 要导入的证书,就是刚才保存的那个,注意使用实际的位置
-file ~/Downloads/root.crt \
# 给证书起个名字
-alias "mavenrepository-mycompany"

执行后 keytool 会询问是否信任这个证书,输入 yes 来信任。

导入完成后,Maven 就可以从这个私库下载依赖了。如果你使用 IDEA 的话,那么可能需要重新启动一下 IDEA 才会使新的 keystore 生效。

如果我的博客帮到了你,那么可不可以请我喝一杯咖啡?

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK