【问题标题】:ExceptionInInitializerError on DriverManager with MS SQL Server带有 MS SQL Server 的 DriverManager 上的 ExceptionInInitializerError
【发布时间】:2014-11-02 17:05:19
【问题描述】:

我正在尝试使用 JDBC 连接到 MS SQL Server DB,并且在这一行:

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)。 我还下载了“JCE Unlimited Strength Jurisdiction Policy Files”并将它们放在 JDK 中。我也关注了这个link,但我不确定我是否应该干预 JDK 的“java.policy”文件......

【问题讨论】:

  • 你能显示url的值吗?
  • urllocalhost
  • 所以connectionUrl"jdbc:localhost;user=dummy;password=dummy;"?这不可能是正确的。
  • 正确语法:String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=AdventureWorks;user=UserName;password=*****";
  • 您从哪里下载了 JCE 文件?它们适用于 java 8 吗?

标签: java sql-server jdbc securityexception


【解决方案1】:

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

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

尝试使用:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 2017-04-29
    • 2011-06-12
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    相关资源
    最近更新 更多