Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Connecting to a Database in SSL Mode

Updated on 2025-02-27 GMT+08:00

When establishing connections to the GaussDB server using JDBC, you can enable SSL connections to encrypt client and server communications for security of sensitive data transmission on the Internet. You can connect to a database in SSL mode using the NonValidatingFactory channel or certificate-based authentication. In certificate-based authentication mode, a client and a server authenticate each other. In this section, the DriverManager.getConnection(String url, Properties info) API is used to connect to a database.

Method 1: NonValidatingFactory Channel

Prerequisites: You have obtained the certificates and private key file required by the server and configured the server. For details about how to generate and obtain the certificates and configure the server, contact an administrator or refer to related OpenSSL documents and commands.

Connect to a database through the NonValidatingFactory channel as follows:

  1. Import java.sql.Connection, java.sql.DriverManager, and java.util.Properties.

    In addition, you need to import other APIs and classes based on the actual application scenario. For details, see JDBC Interface Reference.
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.util.Properties;

  2. Specify the database sourceURL (change $ip, $port, and database as required), username, and password.

    Writing the username and password to code has great security risks. You are advised to store the username and password in environment variables.

    String sourceURL = "jdbc:opengauss://$ip:$port/database";
    Properties urlProps = new Properties();
    urlProps.setProperty("user", System.getenv("EXAMPLE_USERNAME_ENV"));
    urlProps.setProperty("password", System.getenv("EXAMPLE_PASSWORD_ENV"));

  3. Set the SSL property to true to use the NonValidatingFactory channel.

    urlProps.setProperty("ssl", "true");
    urlProps.setProperty("sslfactory","com.huawei.opengauss.jdbc.ssl.NonValidatingFactory");

  4. Load the driver.

    1. Add the opengaussjdbc.jar package to the code running tool (such as IDE).
    2. Load the database driver com.huawei.opengauss.jdbc.Driver as follows:
    Class.forName("com.huawei.opengauss.jdbc.Driver");

  5. Establish a database connection.

    Call DriverManager.getConnection(String url, Properties info) to connect to the database.
    Connection conn = DriverManager.getConnection(sourceURL,urlProps);

Method 2: Certificate-based Authentication

Prerequisites: You have obtained the certificates and private key file required by the server and configured the server. You have obtained the client.crt client certificate, cacert.pem root certificate, and client.key.pk8 client private key file required by the client. Step 3 describes how to configure the certificates and private key file on the client. For details about how to generate and obtain the certificates and configure the server, contact an administrator or refer to related OpenSSL documents and commands.

Configure certificates on the client to connect to a database as follows:

  1. Import java.sql.Connection, java.sql.DriverManager, and java.util.Properties.

    In addition, you need to import other APIs and classes based on the actual application scenario. For details, see JDBC Interface Reference.
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.util.Properties;

  2. Specify the database sourceURL (change $ip, $port, and database as required), username, and password.

    Writing the username and password to code has great security risks. You are advised to store the username and password in environment variables.

    String sourceURL = "jdbc:opengauss://$ip:$port/database";
    Properties urlProps = new Properties();
    urlProps.setProperty("user", System.getenv("EXAMPLE_USERNAME_ENV"));
    urlProps.setProperty("password", System.getenv("EXAMPLE_PASSWORD_ENV"));

  3. Set the SSL property to true and configure the client.crt client certificate, cacert.pem root certificate, and client.key.pk8 client private key on the client.

    urlProps.setProperty("ssl", "true");
    urlProps.setProperty("sslcert", "client.crt");
    urlProps.setProperty("sslrootcert", "cacert.pem");
    urlProps.setProperty("sslkey", "client.key.pk8");
    NOTE:

    Before using the client private key file, convert client.key to client.key.pk8.

    /**
     * openssl pkcs8 -topk8 -outform DER -in client.key -out client.key.pk8 -nocrypt
     * openssl pkcs8 -topk8 -inform PEM -in client.key -outform DER -out client.key.der -v1 PBE-MD5-DES
     * openssl pkcs8 -topk8 -inform PEM -in client.key -outform DER -out client.key.der -v1 PBE-SHA1-3DES
     * The preceding algorithms are not recommended due to their low security.
     * If the customer needs to use a higher-level private key encryption algorithm, the following private key encryption algorithms can be used after the BouncyCastle or a third-party private key is used to decrypt the password package:
     * openssl pkcs8 -in client.key -topk8  -outform DER -out client.key.der -v2 AES128
     * openssl pkcs8 -in client.key -topk8  -outform DER -out client.key.der -v2 aes-256-cbc -iter 1000000
     * openssl pkcs8 -in client.key -topk8 -out client.key.der  -outform Der -v2 aes-256-cbc -v2prf hmacWithSHA512
     * Enable BouncyCastle: Introduce the bcpkix-jdk15on.jar package for projects that use JDBC. The recommended version is 1.65 or later.
     */

  4. Configure sslmode.

    Set sslmode to require, verify-ca, or verify-full. For details about the parameters, see sslmode. You can select one of them based on the application scenario.
    /* Set sslmode to require. */
    urlProps.setProperty("sslmode", "require");
    /* Set sslmode to verify-ca. */
    urlProps.setProperty("sslmode", "verify-ca");
    /* Set sslmode to verify-full (verification in Linux). */
    urlProps.setProperty("sslmode", "verify-full");

  5. Load the driver.

    1. Add the opengaussjdbc.jar package to the code running tool (such as IDE).
    2. Load the database driver com.huawei.opengauss.jdbc.Driver as follows:
    Class.forName("com.huawei.opengauss.jdbc.Driver");

  6. Establish a database connection.

    Call DriverManager.getConnection(String url, Properties info) to connect to the database.
    Connection conn = DriverManager.getConnection(sourceURL,urlProps);

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback