6

我在加密数据库密码时遇到问题hibernate.cfg.xml

这是我的属性文件。

<!-- Database connection settings -->
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=TEST;</property>
<property name="connection.username">sa</property>
<!-- Encryption -->
<property name="connection.password">ENC(vMO/j5jfpaU2cUhPVoOk5Q==)</property>
<property name="connection.provider_class">org.jasypt.hibernate4.connectionprovider.EncryptedPasswordDriverManagerConnectionProvider</property>
<property name="connection.encryptor_registered_name">hibernateEncryptor</property>

然后在HiberanteUtil.java我有这个

// Builds session factory.
private static SessionFactory configureSessionFactory() 
    throws HibernateException {

  Configuration configuration = new Configuration().configure();
  StandardPBEStringEncryptor encryptor =
      new StandardPBEStringEncryptor();
  encryptor.setPassword("pass");

  HibernatePBEEncryptorRegistry registry =
      HibernatePBEEncryptorRegistry.getInstance();

  registry.registerPBEStringEncryptor("hibernateEncryptor", encryptor);

  ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
      .applySettings(configuration.getProperties()).buildServiceRegistry();

  return configuration.buildSessionFactory(serviceRegistry);
}

我已经创建了加密密码encrypt.bat

然后我的错误是

com.microsoft.sqlserver.jdbc.SQLServerException:用户“sa”登录失败。ClientConnectionId:8033573f-5f52-4fe9-a728-fbe4f57d89c4

如果我删除这部分

StandardPBEStringEncryptor encryptor =
        new StandardPBEStringEncryptor();
encryptor.setPassword("someKey");
HibernatePBEEncryptorRegistry registry =
        HibernatePBEEncryptorRegistry.getInstance();

registry.registerPBEStringEncryptor(
        "hibernateEncryptor", encryptor);

我有同样的错误,所以我认为它没有注册,但我不知道该怎么做。

这就是我加密的方式

jasypt 问题图片

更新

我唯一能让它工作的东西就是这样,但不是我想的那样。

StandardPBEStringEncryptor encryptor =
                new StandardPBEStringEncryptor();
        encryptor.setPassword("somePass");
        encryptor.setAlgorithm("PBEWITHMD5ANDDES");
        String pass=encryptor.decrypt("HhpmA/XmJoLro8TYYu4YyA==");
        HibernatePBEEncryptorRegistry registry =
                HibernatePBEEncryptorRegistry.getInstance();
        registry.registerPBEStringEncryptor(
                "hibernateEncryptor", encryptor);

        Configuration configuration = new Configuration().configure()
                .setProperty("hibernate.connection.encryptor_registered_name","hibernateEncryptor")
                .setProperty("hibernate.connection.password",pass);

所以我认为问题在于"hibernateEncryptor",我想我需要注册

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

但是当我把它hibernate.cfg.xml 说成无效映射时,所以我将它添加到一个带有注释的类中,但什么也没发生,因为我认为这是在我想要加密的数据库连接之后读取的。:(

@TypeDef(name="encryptedString",typeClass=org.jasypt.hibernate4.type.EncryptedStringType.class,
        parameters= {@Parameter(name="encryptorRegisteredName",value="hibernateEncryptor")})
4

3 回答 3

6

这不是正确的方法,但可以解决。

StandardPBEStringEncryptor encryptor =new StandardPBEStringEncryptor();
encryptor.setPassword("somePass");
encryptor.setAlgorithm("PBEWITHMD5ANDDES");
Configuration configuration = new Configuration().configure();
String pass=encryptor.decrypt(configuration.getProperty("hibernate.connection.password"));
configuration.setProperty("hibernate.connection.password",pass);   

而在hibernate.cfg

    <property name="connection.username">sa</property>
    <property name="connection.password">Nzuyhu5PJJwsVH3mdw==</property>
于 2013-09-09T16:24:01.060 回答
2

你可以试试这个:

StandardPBEStringEncryptor strongEncryptor = new StandardPBEStringEncryptor();
strongEncryptor.setPassword("jasypt");
strongEncryptor.setAlgorithm("PBEWITHMD5ANDDES");
HibernatePBEEncryptorRegistry registry =                          HibernatePBEEncryptorRegistry.getInstance();
registry.registerPBEStringEncryptor("strongHibernateStringEncryptor", strongEncryptor);

Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
configuration.setProperty("hibernate.connection.password", strongEncryptor.decrypt(configuration.getProperty("hibernate.connection.password")));
ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
于 2013-09-20T19:03:10.627 回答
0

http://www.jasypt.org/hibernate.html

为什么不将算法切换到:PBEWithMD5AndTripleDES

查看 StackOverflow 上的这篇文章: 使用 Hibernate 3 和 Struts 2 实现 Jasypt 时出错

于 2013-09-14T12:23:07.477 回答