用 spring boot, 并将pem 文件转换成 jks 文件,然后连接 AWS 的 DocumentDB 的详细示例

要使用 Spring Boot 应用程序连接到 AWS DocumentDB,并将 PEM 文件转换成 JKS 文件,需要以下几个步骤:

  1. 创建 Spring Boot 项目
  2. 配置 AWS DocumentDB
  3. 将 PEM 文件转换为 JKS 文件
  4. 配置 Spring Boot 应用程序
  5. 实现连接并验证

以下是详细的步骤和示例代码。

1. 创建 Spring Boot 项目

使用 Spring Initializr(https://start.spring.io/)生成一个包含以下依赖项的项目:

  • Spring Data MongoDB
  • Spring Web

2. 配置 AWS DocumentDB

确保你的 AWS DocumentDB 集群已经创建并可用,并获取连接字符串和所需的证书文件。

3. 将 PEM 文件转换为 JKS 文件

假设你有一个 PEM 文件 rds-combined-ca-bundle.pem

3.1. 安装 OpenSSL 和 Keytool

确保你的系统上安装了 OpenSSL 和 JDK(其中包含 Keytool)。

3.2. 转换 PEM 文件为 PKCS12 文件

openssl pkcs12 -export -in rds-combined-ca-bundle.pem -out rds-ca.p12 -name rds-ca

这会提示你设置导出密码,记住这个密码用于下一步。

3.3. 转换 PKCS12 文件为 JKS 文件

keytool -importkeystore -srckeystore rds-ca.p12 -srcstoretype PKCS12 -destkeystore rds-ca.jks -deststoretype JKS -srcalias rds-ca -destalias rds-ca

这会提示你输入源密码(在上一步中设置的密码)和目标密码。

4. 配置 Spring Boot 应用程序

将生成的 rds-ca.jks 文件放置在你的 Spring Boot 项目的 src/main/resources 目录中。然后编辑你的 application.properties 文件:

spring.data.mongodb.uri=mongodb://<username>:<password>@<cluster-endpoint>:<port>/<database>?ssl=true&sslInvalidHostNameAllowed=true
spring.data.mongodb.ssl.enabled=true
spring.data.mongodb.ssl.key-store=classpath:rds-ca.jks
spring.data.mongodb.ssl.key-store-password=<jks-password>

<username><password><cluster-endpoint><port><database><jks-password> 替换为你的 AWS DocumentDB 的相应值和 JKS 密码。

5. 实现连接并验证

5.1 创建一个模型类

package com.example.demo;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "person")
public class Person {
    @Id
    private String id;
    private String name;
    private int age;

    // Getters and Setters
}

5.2 创建一个存储库接口

package com.example.demo;

import org.springframework.data.mongodb.repository.MongoRepository;

public interface PersonRepository extends MongoRepository<Person, String> {
}

5.3 创建一个控制器

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/persons")
public class PersonController {

    @Autowired
    private PersonRepository personRepository;

    @GetMapping
    public List<Person> getAllPersons() {
        return personRepository.findAll();
    }

    @PostMapping
    public Person createPerson(@RequestBody Person person) {
        return personRepository.save(person);
    }
}

启动 Spring Boot 应用程序

确保 src/main/resources 目录中有 rds-ca.jks 文件,然后启动你的 Spring Boot 应用程序:

mvn spring-boot:run

验证连接

通过 POST 和 GET 请求来测试你的控制器。你可以使用 curl 或 Postman 等工具来发送请求。

7.1 创建一个新的 Person

curl -X POST http://localhost:8080/persons -H "Content-Type: application/json" -d '{"name": "John Doe", "age": 30}'

7.2 获取所有 Person

curl http://localhost:8080/persons

注意事项

  • 证书有效性:确保 PEM 文件中的证书是有效的,并且导出时没有错误。
  • 密码管理:确保在转换过程中设置并使用强密码。
  • 路径配置:确保 application.properties 中的路径正确指向你的 JKS 文件。
  • 依赖库:确保 Spring Data MongoDB 版本与 MongoDB 驱动程序兼容。

通过以上步骤,你应该能够使用 Spring Boot 应用程序安全地连接到 AWS 的 DocumentDB。

posted @   gongchengship  阅读(290)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示