我在我的应用程序中使用 Spring 3.1 + Hibernate 4.1。我希望使用 Jasypt 以加密形式存储某些字段(例如密码)。但在集成中我面临以下异常:
java.lang.AbstractMethodError: org.jasypt.hibernate.type.EncryptedStringType.nullSafeSet(Ljava/sql/PreparedStatement;Ljava/lang/Object;ILorg/hibernate/engine/spi/SessionImplementor;)V
为了将 spring+hibernate 与 Jasypt 集成,我遵循了以下步骤:
在lib文件夹中添加了jasypt-1.8.jar。
在dispatcher-servlet(配置文件)中添加了以下内容
< bean id="hibernateStringEncryptor" 类=“org.jasypt.hibernate.cryptor.HibernatePBEStringEncryptor” 惰性初始化=“假”> hibernate字符串加密器 贾西普特 bean >
在实体上放置了以下代码:
@TypeDef( 名称=“加密字符串”, typeClass=EncryptedStringType.class, 参数= { @Parameter(名称=“加密器注册名称”,值=“hibernateStringEncryptor”) } )
但是没有得到预期的结果。请帮我解决这个异常。
问候,
阿伦·库马尔
最佳答案
根据 EncryptedStringType 的 javadocs :
A Hibernate 3 UserType implementation which allows transparent encryption of String values during persistence of entities.
根据Hibernate 4 Migration Guide :
References to org.hibernate.usertype.UserType methods should be changed as indicated:
nullSafeGet(ResultSet rs, String[] names, Object owner) should be changed to
nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) nullSafeSet(PreparedStatement st, Object value, int index) should be changed to nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
所以 jasypt 1.8 与 Hibernate 4.x 不兼容。
您必须升级到 jasypt 1.9,根据this提供 Hibernate 4 支持。
编辑:
Jasypt documentation提供了很好的概述。
第一句话:
Jasypt provides the jasypt-hibernate3 and jasypt-hibernate4 artifacts for Hibernate integration. Since jasypt 1.9.0, these artifacts must be added to your classpath separately.
因此,您需要将 jasypt-hibernate4.jar 添加到类路径中以解决编译错误。
关于spring - Jasypt 与 Hibernate 4 和 spring 3.1 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15362619/