【问题标题】:Connect SpringBoot to MySQL hosted in the cloud requires SSL将 SpringBoot 连接到云中托管的 MySQL 需要 SSL
【发布时间】:2017-06-05 04:59:17
【问题描述】:

我成功地使用 MySQL Workbench 在 Bluemix 托管的 MySQL Compose 服务上完成了完整的 crud。

然后,我使用 SpringBoot 在本地笔记本电脑上使用 Apache Derby 构建了一个简单的微服务...成功。

我的下一步是使用托管在 Bluemix 中的 MySQL Compose。

我编辑了 application.properties 并遇到了这个错误 “PKIX 路径构建失败:....” "SunCertPathBuilderException: 无法找到请求目标的有效证书路径"

application.properties file
spring.jpa.hibernate.ddl-auto=create
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.datasource.url=jdbc:mysql://somedomain:port/compose?useSSL=true?requireSSL=true
spring.datasource.username=myname
spring.datasource.password=mypassword

Bluemix 在 json 中为我提供了这些凭据:

{
  "db_type": "mysql",
  "name": "bmix-dal-yp-xxxxxxx-",
  "uri_cli": "mysql -u myname -p --host somedomain.com --port 5555 --ssl-mode=REQUIRED",
  "ca_certificate_base64": "LS0tLS1CRUd......",
  "deployment_id": "58fexxxxxxxxxxx",
  "uri": "mysql://myname:mypassword@somedomain.com:55555/compose"
}

我应该在 application.properties 的某处使用 ca 证书吗?

我需要在默认使用 springBoot 运行的嵌入式 tomcat 服务器上启用 ssl 吗?

如何配置我的 springBoot 应用程序以使用 SSL 和他们提供的 json 连接到我的云提供商 MySQL 实例?

Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

【问题讨论】:

    标签: mysql ssl spring-boot ibm-cloud compose-db


    【解决方案1】:

    将以下内容添加到您的 pom.xml(或等效项)中:

    ...
    <repositories>
       <repository>
          <id>jcenter</id>
          <url>http://jcenter.bintray.com </url>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
          </snapshots>
           <releases>
             <enabled>true</enabled>
             <checksumPolicy>warn</checksumPolicy>
          </releases>
       </repository>
    </repositories> 
    ...
    <dependency>
       <groupId>com.orange.clara.cloud.boot.ssl-truststore-gen</groupId>
       <artifactId>spring-boot-ssl-truststore-gen</artifactId>
       <version>2.0.21</version>
    </dependency>
    ...
    

    将以下内容添加到您的 manifest.yml

    env:    
        # Add the certificate from VCAP_SERVICES ca_certificate_base64
        # You need to base64 decode the certificate and add it below
        # E.g. echo '<<ca_certificate_base64>>' | base64 -D
    
        TRUSTED_CA_CERTIFICATE: |-
            -----BEGIN CERTIFICATE-----
            ...
            -----END CERTIFICATE-----
    

    欲了解更多信息,请参阅https://github.com/orange-cloudfoundry/spring-boot-ssl-truststore-gen

    还可以在此处查看最小应用程序:https://github.com/snowch/hello-spring-cloud/tree/8b9728a826dcc1995a7ccb19a852ac8face21147


    这是我的第一个答案 - 这不起作用。忽略此部分。

    一个选项是:

    将证书导入Java信任库文件,将文件打包成Java 应用程序并通过 JAVA_OPTS 环境变量指定其路径; 信任库文件可以放在资源目录下。这个可以 用于单个应用程序:

    • 通过使用“cf set-env”命令:

      cf set-env <app> JAVA_OPTS '-Djavax.net.ssl.TrustStore=classpath:resources/config/truststore -Djavax.net.ssl.trustStorePassword=changeit' 
      
    • 或者,通过使用 manifest.yml

      applications:
      - name: java-app
        ...
        env:
            JAVA_OPTS: '-Djavax.net.ssl.TrustStore=classpath:resources/config/truststore -Djavax.net.ssl.trustStorePassword=changeit'
      

    请注意,ca_certificate_base64 字段中的证书是 base64 编码的,因此您需要在将其添加到您的信任库之前对其进行解码,例如

    解码证书:

    echo '<<ca_certificate_base64>>' | base64 -D  > ca_certificate.pem
    

    创建信任库:

    keytool -import -trustcacerts -file ca_certificate.pem -alias compose_cert -keystore resources/config/truststore -storepass changeit -noprompt
    

    请注意,密钥库位置 (resources/config/truststore) 和 storepass (changeit) 是在 JAVA_OPTS 中设置的。

    您可以尝试几种不同的选择。有关详细信息,请参阅此文档:https://discuss.pivotal.io/hc/en-us/articles/223454928-How-to-tell-application-containers-running-Java-apps-to-trust-self-signed-certs-or-a-private-or-internal-CA

    【讨论】:

    • 问题:我应该把提供给我的“ca_certificate_base64”解码成一个名为“truststore”的文件,然后放到我的资源文件夹(SpringBoot 项目)中?
    • 我已经更新了答案。请让我知道你是怎么过的。如果这不起作用,也许您可​​以在 github 中创建一个没有 ssl 的最小示例,我可以尝试添加 ssl 并验证修复工作。
    • 我将 jdbc 更改为不使用 ssl,并且可以从我的笔记本电脑(springBoot 项目)对云数据库进行完整的操作...成功我按照您编写的所有步骤创建了密钥库,但对 JAVA_OPTS 环境变量有点困惑。是否有我需要编辑的 spring application.properties 以便 Tomcat 加载环境变量,或者我是否需要在我的 O/S(笔记本电脑)中创建一个“全局”环境变量。我正在使用 Ubuntu Linux 工作站。
    • 好东西。您的项目在公共 github 存储库中可用吗?如果是,我可以按照我的回答中的说明验证它们是否适用于您的特定项目。
    • 它不在github中。我不确定将 JAVA_OPTS 放在哪里,它在我的 application.properties 文件中吗?发送到服务器的最后一个数据包是 363 毫秒前。
    猜你喜欢
    • 2015-11-29
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多