Docker-JDK容器内无法访问HTTPS请求

背景

使用 dockerfile 基于 jdk 打包docker镜像, 容器内jdk无法访问https请求。

报错: unable to find valid certification path to requested target

Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:451)
... 86 common frames omitted

jdk导入https证书

  • 利用chrome浏览器获得公钥证书

    • 点击浏览器导航栏的小锁图标
    • 找到证书选项 - 详细信息
    • 导出crt格式证书
  • 切换到 jre的/lib/security/ 路径下

  • 导入证书

    keytool -import -alias [证书别名] -keystore cacerts -file [证书路径 ]

  • 验证是否导入完成
    keytool -list -keystore cacerts -alias [证书别名]

docker中的jdk导入https证书

  • 这里使用的是 ringcentral/jdk 镜像, jdk_home = /opt/java/

  • 这里的证书文件名称为 hwit-enterprise-ca-1.crt

  • 导入的证书别名为 hwit-enterprise

  • Dockerfile

    FROM ringcentral/jdk:latest 
    WORKDIR /app
    COPY ./target/demo.jar .
    COPY certs/hwit-enterprise-ca-1.crt /opt/java/jre/lib/security/
    EXPOSE 8080
    RUN \
         cd /opt/java/jre/lib/security/ \
         && sh -c '/bin/echo -e "y" | keytool -import -alias hwit-enterprise -file hwit-enterprise-ca-1.crt -keystore cacerts --storepass changeit -v'
    CMD java -jar govern.jar
    

    部分命令解释:

    // 将客户端/服务端公钥导入的服务端/客户端jdk信任库
    // sh -c '/bin/echo -e "y" 相当于输入y命令:是否信任此证书,输入y 
    RUN \ 
          cd /opt/java/jre/lib/security/ \ 
          && sh -c '/bin/echo -e "y" | keytool -import -alias hwit-enterprise -file hwit-enterprise-ca-1.crt -keystore cacerts --storepass changeit -v'
    

你可能感兴趣的:(Docker-JDK容器内无法访问HTTPS请求)