简体   繁体   中英

How to use CA certificate to connect to database in java

Currently I'm connecting to Database that doesn't need a CA certificate as follows:

input = new FileInputStream("path/to/properties/file");
prop.load(input);

Class.forName(prop.getProperty("JDBC_DRIVER"));
conn = DriverManager.getConnection(prop.getProperty("DB_URL"), prop.getProperty("USER"), prop.getProperty("PASS"));

Now the requirements have changed and i have to connect to the database using CA certificate which i have. Now I'm trying the following:

String url = prop.getProperty("DB_URL")+"?verifyServerCertificate=false"+"&useSSL=true"+"&requireSSL=true";
Class.forName(prop.getProperty("JDBC_DRIVER"));
conn = DriverManager.getConnection(url, prop.getProperty("USER"), prop.getProperty("PASS"));

Could you tell me how I can provide the path to the CA certificate and successfully connect to the database.

I was able to figure out how to connect to the database using SSL(CA certificate):

input = new FileInputStream("path/to/properties/file");
    prop.load(input);

    Class.forName(prop.getProperty("JDBC_DRIVER"));
    System.getProperties().setProperty("javax.net.ssl.trustStore",
            "/path_to_certificate");
    String url = prop.getProperty("DB_URL")
            + "?verifyServerCertificate=false" + "&useSSL=true"
            + "&requireSSL=true"; 
    return DriverManager.getConnection(url, prop.getProperty("USER"),
            prop.getProperty("PASS"));

This one worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM