Java代码SSL https 加密连接 sqlserver

3种连接sqlserver方式 URL 的区别:

1)jdbc:sqlserver://IP:port;DatabaseName=dbName;autoReconnectForPools=true; 

2)jdbc:sqlserver://IP:port;DatabaseName=dbName;autoReconnectForPools=true;encrypt=true;trustServerCertificate=true;  

3)jdbc:sqlserver://IP:port;DatabaseName=dbName;autoReconnectForPools=true;encrypt=true;trustServerCertificate=false;trustStore=/path/to/truststore.ks;trustStorePassword=12345678;hostNameInCertificate=cer证书里的域名

接下来我们说说这3种连接方式的区别:

一、非加密形式的通用连接方式

jdbc:sqlserver://IP:port;DatabaseName=GatewayV7QA;autoReconnectForPools=true; 

二、无条件信任任何根证书的连接方式

jdbc:sqlserver://IP:port;DatabaseName=GatewayV7QA;autoReconnectForPools=true;encrypt=true;trustServerCertificate=true;  
trustServerCertificate=true;  这个参数 true 表示无条件信任server端返回的任何根证书

三、客户端需验证server端SSL证书的连接方式

jdbc:sqlserver://IP:port;DatabaseName=dbName;autoReconnectForPools=true;encrypt=true;trustServerCertificate=false;trustStore=/path/to/truststore.ks;trustStorePassword=12345678;hostNameInCertificate=cer证书里的域名

end.

四、报错解决思路

4.1、InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty  

如果是这个错误,表示ks证书路径不对或者ks证书无效,也就是 trustStore=/path/to/truststore.ks 这个值设置错了

4.2、no sqljdbc_auth in java.library.path 或者 sqljdbc_auth.dll: unknown file type, first eight bytes: 0x4D 0x5A 0x90 0x00 0x03 0x00 0x00 0x00

这种找 dll 错误的貌似都是发生在windows端的错误,但是我这里证明了是因为连 trustStore、trustStorePassword、hostNameInCertificate 这3个参数都没配置所以才会报错

4.3、SQLServerException: This driver is not configured for integrated authentication

 trustStore、trustStorePassword、hostNameInCertificate 这3个参数都没配置所以才会报错

猜你喜欢

转载自www.cnblogs.com/zhuwenjoyce/p/12469793.html