云数据库 GAUSSDB NOSQL-通过Java语言连接实例:使用SSL方式连接实例的Java代码示例

时间:2023-11-10 12:13:23

使用SSL方式连接实例的Java代码示例

package influxdb;

 import java.security.SecureRandom;
 import java.security.cert.X509Certificate;
 import java.util.concurrent.TimeUnit;
 import javax.net.ssl.SSLContext;

 import okhttp3.OkHttpClient;
 import org.influxdb.InfluxDB;
 import org.influxdb.InfluxDBFactory;
 import org.influxdb.dto.Point;
 import org.influxdb.dto.Query;
 import org.influxdb.dto.QueryResult;

 import org.apache.http.ssl.SSLContexts;
 import javax.net.ssl.*;

 public class demo {
     public static void main(String[] args) {
         OkHttpClient.Builder client = new OkHttpClient.Builder()
             .connectTimeout(10, TimeUnit.SECONDS)
             .writeTimeout(10, TimeUnit.SECONDS)
             .readTimeout(10, TimeUnit.SECONDS)
             .retryOnConnectionFailure(true);

         client.sslSocketFactory(defaultSslSocketFactory(), defaultTrustManager());
         client.hostnameVerifier(noopHostnameVerifier());

         final String serverURL = "https://127.0.0.1:8086", username = "root", password = "root";

         InfluxDB influxdb = InfluxDBFactory.connect(serverURL, username, password, client);

         // Create a database...
         String databaseName = "foo";
         influxdb.query(new Query("CREATE DATABASE " + databaseName, databaseName));
         influxdb.setDatabase(databaseName);

         // Write points to influxdb.
         influxdb.write(Point.measurement("bar")
             .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
             .tag("location", "chengdu")
             .addField("temperature", 22)
             .build());

         // Query your data using InfluxQL.
         QueryResult queryResult = influxdb.query(new Query("SELECT * FROM bar", databaseName));

         // Close it if your application is terminating or you are not using it anymore.
         influxdb.close();
     }

     private static X509TrustManager defaultTrustManager() {
         return new X509TrustManager() {
             public X509Certificate[] getAcceptedIssuers() {
                 return new X509Certificate[0];
             }

             public void checkClientTrusted(X509Certificate[] certs, String authType) {
             }

             public void checkServerTrusted(X509Certificate[] certs, String authType) {
             }
         };
     }

     private static SSLSocketFactory defaultSslSocketFactory() {
         try {
             SSLContext sslContext = SSLContexts.createDefault();

             sslContext.init(null, new TrustManager[] {
                 defaultTrustManager()
             }, new SecureRandom());
             return sslContext.getSocketFactory();
         } catch (Exception e) {
             throw new RuntimeException(e);
         }

     }

     private static HostnameVerifier noopHostnameVerifier() {
         return new HostnameVerifier() {
             @Override
             public boolean verify(final String s, final SSLSession sslSession) { 
                 return true;//true 表示使用ssl方式,但是不校验ssl证书,建议使用这种方式
             }
         };
     }
 }
support.huaweicloud.com/productdesc-nosql/nosql_09_0101.html