2

我尝试使用 jasypt 和 hibernate 加密数据库内容。我总是看到这个错误消息。我正在使用 maven、hibernate 4 和 jasypt 1.9.1。如果您需要更多详细信息,请告诉我。任何人都可以帮助我吗?

谢谢你们

Exception in thread "AWT-EventQueue-0" org.hibernate.MappingException: Could not determine type for: org.jasypt.hibernate.type.EncryptedStringType, at table: User, for columns: [org.hibernate.mapping.Column(password)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:304)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:288)
at org.hibernate.mapping.Property.isValid(Property.java:238)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:468)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1290)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1732)
at stnik.data.DBConnection.<init>(DBConnection.java:11)
at stnik.data.DBConnection.getInstance(DBConnection.java:21)
at stnik.data.PatientDal.getAll(PatientDal.java:16)
at stnik.service.PatientService.getAllTableFormatted(PatientService.java:23)
at stnik.gui.JIPatientList.<init>(JIPatientList.java:75)
at stnik.gui.GuiFactory.getGui(GuiFactory.java:23)
at stnik.gui.MainGui$2.actionPerformed(MainGui.java:91)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我的 User.java 文件中的代码:

package stnik.bo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.hibernate.annotations.Type;

@Entity
public class User {
private int userID;
private String username;
private String password;

public User() {
}

public User(int userID, String username, String password) {
    super();
    this.userID = userID;
    this.username = username;
    this.password = password;
}

public User(String username, String password) {
    super();
    this.username = username;
    this.password = password;
}

@Id @GeneratedValue(strategy = GenerationType.AUTO)
public int getUserID() {
    return userID;
}

public void setUserID(int userID) {
    this.userID = userID;
}

@Column(name="username", length=250)
@Type(type="encryptedString")
public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

@Column(name="password", length=250)
@Type(type="encryptedString")
public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

}

休眠.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost/patientenverwaltung</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password"></property>
  <property name="hibernate.connection.pool_size">10</property>

  <property name="show_sql">true</property>

  <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

  <property name="transaction.factory_class">
     org.hibernate.transaction.JDBCTransactionFactory
  </property>

  <property name="hibernate.cache.provider_class">
     org.hibernate.cache.HashtableCacheProvider
  </property>

  <property name="hibernate.hbm2ddl.auto">update</property>

  <!-- Mapping files -->
  <mapping resource="Patient.hbm.xml"/>
  <mapping resource="Doctor.hbm.xml"/>
  <mapping resource="MedicalReport.hbm.xml"/>
  <mapping resource="SpecifiedMedicalRecord.hbm.xml"/>
  <mapping resource="jasyptHibernateTypes.hbm.xml"/>
  <mapping class="stnik.bo.User"/>
</session-factory>
</hibernate-configuration>

jasyptHibernateTypes.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<!-- VARCHAR, CLOB, TEXT based types -->

<typedef name="encryptedString" class="org.jasypt.hibernate.type.EncryptedStringType">
    <param name="encryptorRegisteredName">jasyptHibernateEncryptor</param>
</typedef>

<typedef name="encryptedBigDecimalAsString"
    class="org.jasypt.hibernate.type.EncryptedBigDecimalAsStringType">
    <param name="encryptorRegisteredName">jasyptHibernateEncryptor</param>
</typedef>

<typedef name="encryptedBigIntegerAsString"
    class="org.jasypt.hibernate.type.EncryptedBigIntegerAsStringType">
    <param name="encryptorRegisteredName">jasyptHibernateEncryptor</param>
</typedef>

<typedef name="encryptedBooleanAsString"
    class="org.jasypt.hibernate.type.EncryptedBooleanAsStringType">
    <param name="encryptorRegisteredName">jasyptHibernateEncryptor</param>
</typedef>

<typedef name="encryptedByteAsString"
    class="org.jasypt.hibernate.type.EncryptedByteAsStringType">
    <param name="encryptorRegisteredName">jasyptHibernateEncryptor</param>
</typedef>

<typedef name="encryptedCalendarAsString"
    class="org.jasypt.hibernate.type.EncryptedCalendarAsStringType">
    <param name="encryptorRegisteredName">jasyptHibernateEncryptor</param>
</typedef>

<typedef name="encryptedDateAsString"
    class="org.jasypt.hibernate.type.EncryptedDateAsStringType">
    <param name="encryptorRegisteredName">jasyptHibernateEncryptor</param>
</typedef>

<typedef name="encryptedDoubleAsString"
    class="org.jasypt.hibernate.type.EncryptedDoubleAsStringType">
    <param name="encryptorRegisteredName">jasyptHibernateEncryptor</param>
</typedef>

<typedef name="encryptedFloatAsString"
    class="org.jasypt.hibernate.type.EncryptedFloatAsStringType">
    <param name="encryptorRegisteredName">jasyptHibernateEncryptor</param>
</typedef>

<typedef name="encryptedIntegerAsString"
    class="org.jasypt.hibernate.type.EncryptedIntegerAsStringType">
    <param name="encryptorRegisteredName">jasyptHibernateEncryptor</param>
</typedef>

<typedef name="encryptedLongAsString"
    class="org.jasypt.hibernate.type.EncryptedLongAsStringType">
    <param name="encryptorRegisteredName">jasyptHibernateEncryptor</param>
</typedef>

<typedef name="encryptedShortAsString"
    class="org.jasypt.hibernate.type.EncryptedShortAsStringType">
    <param name="encryptorRegisteredName">jasyptHibernateEncryptor</param>
</typedef>


<!-- VARBINARY, BLOB based type -->
<typedef name="encryptedBinary" class="org.jasypt.hibernate.type.EncryptedBinaryType">
    <param name="encryptorRegisteredName">jasyptByteHibernateEncryptor</param>
</typedef>

<!-- NUMERIC, NUMBER based types -->
<typedef name="encryptedBigDecimal"
    class="org.jasypt.hibernate.type.EncryptedBigDecimalType">
    <param name="encryptorRegisteredName">jasyptBigDecimalHibernateEncryptor</param>
</typedef>

<typedef name="encryptedBigInteger"
    class="org.jasypt.hibernate.type.EncryptedBigIntegerType">
    <param name="encryptorRegisteredName">jasypBigIntegertHibernateEncryptor</param>
</typedef>



</hibernate-mapping>

和 applicationContext.xml:

<bean id="stringEncryptor"  class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor" lazy-init="false">
<property name="algorithm" value="PBEWithMD5AndDES" />
<property name="password" value="ccffe2c10f9882e378d62740bdc3883665aa85eaba07a8e57a98ab42ad" />
</bean>

<bean id="hibernateEncryptor" class="org.jasypt.hibernate.encryptor.HibernatePBEStringEncryptor" lazy-init="false">
<!-- This property value must match "encryptorRegisteredName" used when defining hibernate user types -->
<property name="registeredName" value="jasyptHibernateEncryptor" />
<property name="encryptor" ref="stringEncryptor" />
</bean>
4

1 回答 1

1

我认为休眠无法识别 jasyptHibernateTypes.hbm.xml 文件中定义的“encryptedString”,

您可以尝试使用以下注释在实体类本身中添加此“encryptedString”类型定义吗?

import org.hibernate.annotations.TypeDefs;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.Parameter;

@TypeDefs({
        @TypeDef(name = "encryptedString", typeClass = org.jasypt.hibernate.type.EncryptedStringType.class, parameters = { @Parameter(name = "encryptorRegisteredName", value = "jasyptHibernateEncryptor") }) 
        })

@Entity
public class User {

....

}
于 2015-12-30T10:20:45.043 回答