本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
透過 設定SSL連線 JDBC
若要透過 使用SSL連線JDBC,您必須建立金鑰存放區、信任 Amazon RDS根 CA 憑證,並使用下列指定的程式碼片段。
若要以 JKS 格式建立金鑰存放區,您可以使用下列命令。如需建立金鑰存放區的詳細資訊,請參閱 Oracle 文件中的建立金鑰存放區
keytool -genkey -alias
client
-validity365
-keyalgRSA
-keystoreclientkeystore
採取下列步驟信任 Amazon RDS 根 CA 憑證。
信任 Amazon RDS 根 CA 憑證
-
下載適用於所有 AWS 區域 的憑證套件 .pem 檔案,並將檔案放入 ssl_wallet 目錄中。
如需有關下載憑證的詳細資訊,請參閱使用 SSL/TLS 加密資料庫執行個體或叢集的連線。
-
使用作業系統公用程式,將 .pem 檔案中的每個憑證解壓縮到個別檔案中。
-
使用單獨的
openssl
命令將每個憑證轉換為 .der 格式,取代certificate-pem-file
憑證 .pem 檔案的名稱 (不含 .pem 副檔名)。openssl x509 -outform der -in
certificate-pem-file
.pem -outcertificate-pem-file
.der -
使用下列命令將每個憑證匯入金鑰存放區。
keytool -import -alias rds-root -keystore
clientkeystore.jks
-filecertificate-pem-file
.der如需詳細資訊,請參閱輪換您的 SSL/TLS 憑證。
-
確認金鑰存放區已成功建立。
keytool -list -v -keystore
clientkeystore.jks
在提示出現時,請輸入金鑰存放區密碼。
下列程式碼範例示範如何使用 設定SSL連線JDBC。
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class OracleSslConnectionTest { private static final String DB_SERVER_NAME = "
dns-name-provided-by-amazon-rds
"; private static final Integer SSL_PORT = "ssl-option-port-configured-in-option-group
"; private static final String DB_SID = "oracle-sid
"; private static final String DB_USER = "user-name
"; private static final String DB_PASSWORD = "password
"; // This key store has only the prod root ca. private static final String KEY_STORE_FILE_PATH = "file-path-to-keystore
"; private static final String KEY_STORE_PASS = "keystore-password
"; public static void main(String[] args) throws SQLException { final Properties properties = new Properties(); final String connectionString = String.format( "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCPS)(HOST=%s)(PORT=%d))(CONNECT_DATA=(SID=%s)))", DB_SERVER_NAME, SSL_PORT, DB_SID); properties.put("user", DB_USER); properties.put("password", DB_PASSWORD); properties.put("oracle.jdbc.J2EE13Compliant", "true"); properties.put("javax.net.ssl.trustStore", KEY_STORE_FILE_PATH); properties.put("javax.net.ssl.trustStoreType", "JKS"); properties.put("javax.net.ssl.trustStorePassword", KEY_STORE_PASS); final Connection connection = DriverManager.getConnection(connectionString, properties); // If no exception, that means handshake has passed, and an SSL connection can be opened } }
注意
指定此處所顯示提示以外的密碼,作為安全最佳實務。