带有MS SQL Server的DriverManager上的ExceptionInInitializerError

时间:2014-11-02 17:05:20

标签: java sql-server jdbc securityexception

我尝试使用JDBC并在此行上连接到MS SQL Server数据库:

String connectionUrl = "jdbc:" + url + ";user=dummy;password=dummy;";
conn = DriverManager.getConnection(connectionUrl);

我得到了这个巨大的错误文本:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at javax.crypto.JceSecurityManager.<clinit>(JceSecurityManager.java:65)
    at javax.crypto.Cipher.getConfiguredPermission(Cipher.java:2543)
    at javax.crypto.Cipher.getMaxAllowedKeyLength(Cipher.java:2567)
    at sun.security.ssl.CipherSuite$BulkCipher.isAvailable(CipherSuite.java:548)
    at sun.security.ssl.CipherSuite$BulkCipher.isAvailable(CipherSuite.java:527)
    at sun.security.ssl.CipherSuite.isAvailable(CipherSuite.java:194)
    at sun.security.ssl.SSLContextImpl.getApplicableCipherSuiteList(SSLContextImpl.java:350)
    at sun.security.ssl.SSLContextImpl.getDefaultCipherSuiteList(SSLContextImpl.java:308)
    at sun.security.ssl.SSLSocketImpl.init(SSLSocketImpl.java:607)
    at sun.security.ssl.SSLSocketImpl.<init>(SSLSocketImpl.java:549)
    at sun.security.ssl.SSLSocketFactoryImpl.createSocket(SSLSocketFactoryImpl.java:110)
    at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1606)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1323)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at BusinessManager.Session.<init>(Session.java:33)
    at BusinessManager.BusinessManager.getSession(BusinessManager.java:172)
    at example.Example.ConfigureEnvironment(Example.java:198)
    at example.Example.main(Example.java:50)
Caused by: java.lang.SecurityException: Can not initialize cryptographic mechanism
    at javax.crypto.JceSecurity.<clinit>(JceSecurity.java:89)
    ... 22 more
Caused by: java.lang.SecurityException: Cannot locate policy or framework files!
    at javax.crypto.JceSecurity.setupJurisdictionPolicies(JceSecurity.java:254)
    at javax.crypto.JceSecurity.access$000(JceSecurity.java:48)
    at javax.crypto.JceSecurity$1.run(JceSecurity.java:81)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.crypto.JceSecurity.<clinit>(JceSecurity.java:78)
    ... 22 more

我已经关注了几个链接,确保服务器和客户端都使用相同的JDK(1.8.0_20)。 我还下载了&#34; JCE Unlimited Strength Jurisdiction Policy Files&#34;并将它们放在JDK中。我也跟着这个link,但我不确定我是否应该干涉JDK&#34; java.policy&#34;文件...

1 个答案:

答案 0 :(得分:2)

我根据错误猜测的是,您尝试使用SSL加密会话连接到SQL,但在SQL Server上使用自签名证书。每Connecting with SSL Encryption

  

当encrypt属性设置为true时   trustServerCertificate属性设置为true,即Microsoft JDBC   SQL Server驱动程序不会验证SQL Server SSL   证书。这通常是允许测试中的连接所必需的   环境,例如SQL Server实例只有self的环境   签署证书。

尝试使用:

String connectionUrl = "jdbc:" + url + ";user=dummy;password=dummy;encrypt=true;trustServerCertificate=true;";

此外,您的url变量内容应以sqlserver://开头。