
# 以SSL方式连接
用户通过JDBC连接GaussDB服务器时，可以通过开启SSL加密客户端和服务器之间的通讯，为敏感数据在Internet上的传输提供一种安全保障手段。
本节主要介绍应用程序通过JDBC如何采用SSL的方式对客户端进行配置（服务端配置请联系管理员）。
在使用本节所描述的方法前，默认用户已经获取了服务端和客户端所需要的证书和私钥文件。关于证书生成和获取，请联系管理员或参见Openssl相关文档和命令。
#### 客户端配置
获取client.key、client.crt、cacert.pem证书文件，用于之后操作的配置。
设置服务端身份验证，需要使用到已生成的服务端cacert.pem证书文件。
1. 使用Java的keytool工具导入服务器证书。 
   ```
   >keytool -importcert -alias MyCACert -file cacert.pem -keystore truststore -storepass mypassword
   ```
   
   
2. 在Java或者应用程序中配置信任库。可以选择以下方法中的一种。 
   方法一：使用Java命令行参数。
   ```
   -Djavax.net.ssl.trustStore=path_to_truststore_file  
   -Djavax.net.ssl.trustStorePassword=mypassword
   ```
   方法二：在客户端代码中设置系统属性。
   ```
   System.setProperty("javax.net.ssl.trustStore","path_to_truststore_file");
   System.setProperty("javax.net.ssl.trustStorePassword","mypassword");
   ```
   方法三：在连接属性中设置。
   ```
   trustCertificateKeyStoreUrl=file:path_to_truststore_file 
   trustCertificateKeyStorePassword=mypassword
   ```
   
   
设置客户端身份验证，需要使用到已生成的clientcert.pem和clientkey.pem证书文件。
1. 将客户端证书文件client.crt和密钥client.key，转为pem格式。 
   ```
   >openssl x509 -in client.crt -out client.pem -outform PEM
   >openssl rsa -in client.key -out clientkey.pem -outform PEM
   ```
   
   
2. 将客户端密钥和证书文件转换为PKCS12格式。 
   ```
   >openssl pkcs12 -export -in client.pem -inkey clientkey.pem -name "myclient" -passout pass:mypassword -out clientkeystore.p12
   ```
   
   
3. 将客户端密钥和证书导入Java密钥库。 
   ```
   >keytool -importkeystore -srckeystore clientkeystore.p12 -srcstoretype pkcs12 -srcstorepass mypassword -destkeystore keystore -deststoretype JKS -deststorepass mypassword
   ```
   
   
4. 在Java或者应用程序中配置信任库。可以选择以下方法中的一种。 
   方法一：使用Java命令行参数。
   ```
   -Djavax.net.ssl.keyStore=path_to_keystore_file 
   -Djavax.net.ssl.keyStorePassword=mypassword
   ```
   方法二：在客户端代码中设置系统属性。
   ```
   System.setProperty("javax.net.ssl.keyStore","path_to_keystore_file");
   System.setProperty("javax.net.ssl.keyStorePassword","mypassword");
   ```
   方法三：在连接属性中设置。
   ```
   clientCertificateKeyStoreUrl=file:path_to_truststore_file
   clientCertificateKeyStorePassword=mypassword
   ```
   
   
 
#### 示例
```
// 认证用的用户名和密码直接写到代码中有很大的安全风险，建议在配置文件或者环境变量中存放（密码应密文存放，使用时解密），确保安全。
// 本示例以用户名和密码保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量（环境变量名称请根据自身情况进行设置）EXAMPLE_USERNAME_ENV和EXAMPLE_PASSWORD_ENV。
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class SSL{
    public static void main(String[] args) {
        String userName = System.getenv("EXAMPLE_USERNAME_ENV");
        String password = System.getenv("EXAMPLE_PASSWORD_ENV");
        String urls = "jdbc:gaussdb://$ip:$port/database?useSSL=true&verifyServerCertificate=true"+
            "&trustCertificateKeyStoreUrl=file:path_to_truststore_file" +
            "&trustCertificateKeyStorePassword=mypassword"+
            "&clientCertificateKeyStoreUrl=file:path_to_truststore_file" +
            "&clientCertificateKeyStorePassword=mypassword";
 
        // 创建数据库连接。
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Connection conn;
            conn = DriverManager.getConnection(urls,userName, password);
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```
