Maven:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider...

 仍是记录使用 maven 时遇到的问题。html

1、maven报错

  maven package 进行打包时出现了如下报错:
  Non-resolvable parent POM for com.wpbxin:springboot2-first-example:0.0.1-SNAPSHOT: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.1.5.RELEASE from/to alimaven (https://maven.aliyun.com/nexus/content/groups/public): sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target and 'parent.relativePath' points at wrong local POM @ line 10, column 13 -> [Help 2]
  截图以下:java

  再来一个同样可是比较奇葩的截图,这一次基本是处理完本次出现的问题后,笔者想回过头来重现问题而后记录下来(先后间隔3天左右),但却发现,同个打包的 maven 命令,前面还打包不成功,隔了十几秒再次运行命令,却又打包成功了(还有真多是渣渣的网络缘由!毕竟我这里用了手机的热点),一次 package 成功了而后后面的打包又基本没问题了,呃。。。spring

 

2、一些说明

  这个是学习 SpringBoot2 时建立 HelloWorld 工程遇到的问题。先说下相关环境和系统设置:
  win10 + JDK 1.8.0_111 + Apache Maven 3.3.9 + idea2019.1( + 手机共享热点,不排除可能网络问题下载慢)( + 谷歌浏览器77版本的)
  笔者回过头记录这篇文章的时候试了下,默认状况下的maven中央仓库地址:https://repo.maven.apache.org/maven2(好像比用阿里云的镜像库时顺利点,速度也感受还行吧。)。这里贴上本地 maven 的 mirror 设置,这里设置的是国内的阿里云镜像,详情能够参考阿里云公共代理库官网相关说明:https://help.aliyun.com/document_detail/102512.html?spm=a2c40.aliyun_maven_repo.0.0.36183054oSYFKSapache

<mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <mirrorOf>central</mirrorOf>
    <!-- 阿里云公共代理库使用指南:https://help.aliyun.com/document_detail/102512.html?spm=a2c40.aliyun_maven_repo.0.0.36183054oSYFKS -->
    <!-- <url>https://maven.aliyun.com/nexus/content/groups/public</url> -->
    <url>https://maven.aliyun.com/repository/public</url>
</mirror>

  

3、出现问题的缘由和几种解决方法

  看了下报错提示,而后在网上搜了下,大体能够看出是 HTTPS 的证书安全检查问题。想一想也是,如今 HTTPS 在大力推广,而 HTTPS 确实是须要双向验证的。既然遇到了,那总得处理完而后记录下吧,省得再被坑。如下就是笔者关于处理这个问题过程当中的一些参考和验证。如下是4种处理方式:浏览器

一、忽略SSL证书检查

  直接忽略掉SSL证书检查,跳过这个验证,在 maven 打包命令中加上参数 “-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true” 。
  idea中的设置: settings ==> Build,Execution,Deployment ==> Build Tools ==> Maven ==> Runner 中的 VM Options ,将参数填入,截图以下:安全

  Eclipse中的设置:右键项目 ==> Run As ==> Run Configurations... ,在 Maven Build 那一块中的 JRE 栏位中的 VM arguments ,将参数填入,注意这里不一样的参数须要换行,截图以下:springboot

参考连接:使用Maven时出现“jssecacerts PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilde”错https://blog.csdn.net/carrie__yang/article/details/79612385网络

 

二、生成证书并导入到 JRE security 中

  既然用的是阿里云的镜像,那就去官网下个证书瞧一瞧。
  第一步,谷歌浏览器打开网站:https://maven.aliyun.com/mvn/view,左上角中那把小锁,也即截图的红框,maven

  点击“证书(有效)”,旧版的浏览器能够有 View certificate 之类的,反正记住是看证书就行。ide

  弹出来一个框框,点击“详细信息”

  选择“复制到文件(C)...”,而后下一步

  选择格式:

  生成的名称,好比我这里aliyun-maven.cer,路径是 C:\cs-softwares\aliyun-maven.cer ,而后导出来:

  完成证书导出

 

  接下来,使用 keytool 命令导入证书,进入到 JDK 下 jre 下 lib 下的 security 目录,好比个人是 C:\cs-softwares\Java\jdk1.8.0_111\jre\lib\security,而后运行命令 keytool -import -alias aliyun-maven -keystore cacerts -file C:\cs-softwares\aliyun-maven.cer ,以下,证书指纹(证书密钥):changeit
-alias :表示指定证书别名
-file :指定证书文件

  输入 Y 表示确认:

  查看证书,证书密钥一样是 changeit :
keytool -list -keystore cacerts -alias aliyun-maven
  删除证书,证书密钥一样是 changeit:
keytool -delete -alias aliyun-maven -keystore cacerts

  确认安装了证书以后,记得关闭这个cmd窗口,而后重启下idea或者Eclipse,再次进行 maven packge ,到这里应该是OK了。若是仍是不行,报出了错误: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty ,如截图:

  来来来,再加上参数(记得路径能够改成本身的 JDK 安装路径),把刚才的证书用上,参考:The trustAnchors Parameter Must Be Non Emptyhttps://www.techpaste.com/2017/03/trustanchors-parameter-must-non-empty/ ,这下应该妥妥了。(后面OK了以后,也试过删除了阿里云镜像的证书,而后再打包,同样没问题,这里能够先试下直接加参数,有问题再加证书
-Djavax.net.ssl.trustStore=C:\cs-softwares\Java\jdk1.8.0_111\jre\lib\security\cacerts
-Djavax.net.ssl.trustAnchors=C:\cs-softwares\Java\jdk1.8.0_111\jre\lib\security\cacerts

三、使用默认的 maven 中央仓库

  前面提到过默认的 maven 中央仓库: https://repo.maven.apache.org/maven2,使用默认的配置就好了(本地仓库自行设置)。

PS:吐槽下,绝望以后回到了最初的起点,使用默认的配置竟然没啥问题(即便用了 maven 默认的中央仓库,原本是要来重现问题的),而后回过头来想用阿里云 maven 镜像重现问题,就把阿里云中央仓库的证书也删除了,并且使用了阿里云的镜像库,也不加任何参数,此次却顺利用上了,而后把 spring-boot-starter-parent 从 2.1.0.RELEASE 到 2.1.9.RELEASE 都试了个遍,哎呦去咋状况,又都没问题了,或者是有问题的,试了第二遍或者第三遍又行了,这还真有多是个人渣渣网络缘由。。。

四、使用 http 的镜像库

  这就是直接避开了吧,这个方法就没试过了,只是查找问题的过程当中看 stackoverflow 上有提到。

 

4、参考连接

一、使用Maven时出现“jssecacerts PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilde”错https://blog.csdn.net/carrie__yang/article/details/79612385

二、完全解决unable to find valid certification path to requested targethttps://blog.csdn.net/gabriel576282253/article/details/81531746

三、关于gradle Cause: unable to find valid certification path to requested target错误解决方法https://www.chengbinbin.cn/archives/1550795296099/

四、Maven编译打包时报“PKIX path building failed”异常解决方法https://www.iteye.com/blog/truth99-2160540

五、Problems using Maven and SSL behind proxyhttps://stackoverflow.com/questions/25911623/problems-using-maven-and-ssl-behind-proxy

六、Could not transfer artifact from/to central because of InvalidAlgorithmParameterException: the trustAnchors parameter must be non-emptyhttps://stackoverflow.com/questions/37278306/could-not-transfer-artifact-from-to-central-because-of-invalidalgorithmparameter

七、The trustAnchors Parameter Must Be Non Emptyhttps://www.techpaste.com/2017/03/trustanchors-parameter-must-non-empty/

八、Could not transfer artifact (https://repo.maven.apache.org/maven2): Received fatal alert: protocol_version -> [Help 1]https://stackoverflow.com/questions/50946420/could-not-transfer-artifact-https-repo-maven-apache-org-maven2-received-fat