我使用EclipseLink和Java Persistence API连接到本地数据库,但是当我创建EntityManager
对象时,我收到了以下错误:
[EL Severe]: ejb: 2017-06-04 19:29:55.066--ServerSession(1644987969)--Exception [EclipseLink-7107] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Error encountered during string decryption.
Internal Exception: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-7107] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Error encountered during string decryption.
Internal Exception: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
我创建EntityManager
的行的错误堆栈跟踪点:
private EntityManagerFactory factory;
private EntityManager em;
public JpaDatabaseConnection() {
factory = Persistence.createEntityManagerFactory("blogspace");
em = factory.createEntityManager();
}
我已经通过Maven Dependencies和JDBC驱动程序将eclipselink和java持久性API添加到项目类路径中。这是我的persistence.xml文件(位于包含项目包的文件夹中的META-INF目录中):
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="blogspace" transaction-type="RESOURCE_LOCAL">
<class>pl.furman.server.database.entities.User</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/blogspace" />
<property name="javax.persistence.jdbc.user" value="blogserver" />
<property name="javax.persistence.jdbc.password" value="123456" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
</properties>
</persistence-unit>
</persistence>
问题的原因是什么以及如何解决?我知道JDBC驱动程序本身可能正常工作,因为当我故意将错误的密码放入persistence.xml
文件时,我收到有关授权失败的错误。数据库已设置并正常工作,因为我可以创建查询并将数据从shell和eclipse toad扩展插入数据库。
提前感谢您的帮助。
编辑:问题在于persistence.xml中的用户名和/或密码。当我将其更改为不同长度的用户和密码时,连接可以正常工作。但是我仍然不知道为什么会这样。
答案 0 :(得分:2)
我可以通过 EclipseLink 版本 2.6.4 确认奇怪的行为,因为我重新设置了你的设置。这是完整的堆栈跟踪:
Internal Exception: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:815)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:205)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:305)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:337)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:303)
at demo.service.UserService.<init>(UserService.java:14)
at demo.Runner.main(Runner.java:8)
Caused by: Exception [EclipseLink-7107] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Error encountered during string decryption.
Internal Exception: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
at org.eclipse.persistence.exceptions.ValidationException.errorDecryptingPassword(ValidationException.java:894)
at org.eclipse.persistence.internal.security.JCEEncryptor.decryptPassword(JCEEncryptor.java:114)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.updateLogins(EntityManagerSetupImpl.java:2404)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.updateSession(EntityManagerSetupImpl.java:2716)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:710)
... 6 more
Caused by: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:934)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:845)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at org.eclipse.persistence.internal.security.JCEEncryptor.decryptPassword(JCEEncryptor.java:109)
... 9 more
看来,这与org.eclipse.persistence.internal.security.JCEEncryptor
中的a bug有关。在调试时,我发现有一些密码长度导致观察到的行为,例如:为123456
。
现在我的回答很好:这个错误已通过版本 2.6.5-RC1 修复。此外,它还与最新的 2.6.5-RC2 发布候选版本(2017年6月开始提供)一样有效。如果您有Maven项目,请更改 EclipseLink 依赖项的版本字符串,如下所示:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.5-RC2</version>
</dependency>
如果这是一个独立项目,请找到最新的2.6.5-RC2 jar file on Maven Central。只需删除旧的 jar 文件,然后将新文件放在应用程序的类路径中。
顺便说一下,当我将数据库中的PW更改为123456789
以及persistence.xml
时,它甚至可以使用错误的2.6.4版本。奇怪,不是吗?
希望它有所帮助。