今天在处理SQLServer数据库连接时,出现了以下问题: (下边有解决方案)
com.microsoft.sqlserver.jdbc.SQLServerException: 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“RSA premaster secret error”。 ClientConnectionId:d1c3978f-3be0-496f-883d-add6cb86d5c3 at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1745) at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1704) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1401) at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1068) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:904) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:451) at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1014) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.yonyou.migrationtool.bs.dao.SQLServerDao.newConnection(Unknown Source) at com.yonyou.migrationtool.bs.dao.BizDaoHandler.invoke(Unknown Source) at com.sun.proxy.$Proxy2.insertDB(Unknown Source) at com.yonyou.migrationtool.bs.service.impl.AccountServiceImpl.saveDB(Unknown Source) at com.yonyou.migrationtool.bs.service.impl.AccountServiceImpl.transfer(Unknown Source) at com.yonyou.migrationtool.vo.TransferTask.start(Unknown Source) at com.yonyou.migrationtool.bs.service.Scheduler$WorkThread.run(Unknown Source) at java.lang.Thread.run(Unknown Source)Caused by: javax.net.ssl.SSLKeyException: RSA premaster secret error at sun.security.ssl.RSAClientKeyExchange.(Unknown Source) at sun.security.ssl.ClientHandshaker.serverHelloDone(Unknown Source) at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source) at sun.security.ssl.Handshaker.processLoop(Unknown Source) at sun.security.ssl.Handshaker.process_record(Unknown Source) at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1630) ... 15 moreCaused by: java.security.NoSuchAlgorithmException: SunTlsRsaPremasterSecret KeyGenerator not available at javax.crypto.KeyGenerator. (KeyGenerator.java:169) at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223) at sun.security.ssl.JsseJce.getKeyGenerator(Unknown Source) ... 25 more
上网找了好多解决方案,不是凑合试的,就是copy别人的.没有一个像样的方案.找了好多外文网站,也没什么合适的方式.
首先说一下我复现问题的方式:
1.在eclipse里运行我的程序连接SQLServer数据库进行操作,没问题!eclipse用的jdk7-64位.
2.打包成jar,用jre来运行的时候,就会抛出上边的异常.(另外如果大家运行在不同环境或者切换不同jdk的时候,我觉得都可以归类为这种问题)
从异常中可以看到:
RSA premaster secret error
说明这个是加密异常. 那么jdk中对应的包就是sunjce_provider.jar,这个包在{JAVA_HOME}\jre\lib\ext下.
而在jre中的这个包就会抛出异常.那么就干脆用jdk中的这个包替换掉jre中的这个包,问题自然就解决了.
我的这个现象可能不能解决所有类似的问题,但是至少能给大家提供一些建议也就好的