spring - Jasypt 与 Hibernate 4 和 spring 3.1 集成

标签 spring hibernate jasypt

我在我的应用程序中使用 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 集成,我遵循了以下步骤:

  1. 在lib文件夹中添加了jasypt-1.8.jar

  2. dispatcher-servlet(配置文件)中添加了以下内容

    < bean id="hibernateStringEncryptor" 类=“org.jasypt.hibernate.cryptor.HibernatePBEStringEncryptor” 惰性初始化=“假”> hibernate字符串加密器 贾西普特

  3. 实体上放置了以下代码:

    @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/

相关文章:

java - Spring Boot + Thymeleaf 的@WebAppConfiguration 和@ContextConfiguration

java - Spring 框架集合被驱逐

java - spring mvc中intercept-url未授权时重定向

java - 我可以在哪里使用 jasypt 存储加密 key

java - Jasypt - 使用不同的加密器进行加密和解密

java - Spring批处理spring jpa错误CannotCreateTransactionException : Could not open JPA EntityManager for transaction IllegalStateException:

java - hibernate 映射 - 运行 hibernate 配置时出现错误

java - Hibernate Envers - 包括更改发生的日期

java - 没有可用的事务性 EntityManager - 使用 JPA Api,Hibernate Session 出错

java - 如何通过DataSourceBuilder将jasypt加密密码传递给数据库