証明書の生成 1. サーバー側で3つのファイルを生成
root.crt (信頼ルート証明書)
server.crt (サーバー証明書)
server.key (秘密鍵)
秘密鍵の生成(パスワード設定必要) 1 openssl genrsa -des3 -out server.key 2048
パスワードの削除(前のステップで設定したパスワードを入力) 1 openssl rsa -in server.key -out server.key
サーバー証明書の作成 1 openssl req -new -key server.key -days 3650 -out server.crt -x509
実行時に以下の情報を入力:
1 2 3 4 5 6 7 Country Name (2 letter code) [AU]:JP State or Province Name (full name) [Some-State]:Tokyo Locality Name (eg, city) []:Tokyo Organization Name (eg, company) [Internet Widgits Pty Ltd]:cuckooM Organizational Unit Name (eg, section) []:cuckooM Common Name (e.g. server FQDN or YOUR name) []:127.0.0.1 Email Address []:
注意:”Common Name”にはサーバーのIPアドレスまたはドメイン名を記入。
自己署名のため、サーバー証明書を信頼ルート証明書として使用
2. クライアント側で3つのファイルを生成
root.crt (信頼ルート証明書、サーバー側で生成済み)
client.crt (クライアント証明書)
client.key (秘密鍵)
秘密鍵の生成(パスワード設定必要) 1 openssl genrsa -des3 -out client.key 2048
パスワードの削除(前のステップで設定したパスワードを入力) 1 openssl rsa -in client.key -out client.key
クライアント証明書の作成 1 openssl req -new -key client.key -out client.csr
PostgreSQL設定 postgresql.confの編集 1 2 3 4 ssl = on ssl_cert_file = 'server.crt' ssl_key_file = 'server.key' ssl_ca_file = 'root.crt'
pg_hba.confの編集 1 hostssl all all 0.0.0.0/0 cert
Spring Boot接続設定 application.yml 1 2 3 4 5 spring: datasource: url: jdbc:postgresql://localhost:5432/mydb?ssl=true&sslmode=verify-full username: blog password: password
SSL証明書の配置 クライアント証明書をJava KeyStoreに変換:
1 2 openssl pkcs12 -export -out client.p12 -inkey client.key -in client.crt -certfile root.crt keytool -importkeystore -destkeystore client.jks -srckeystore client.p12 -srcstoretype pkcs12
参考資料