Cannot encrypt password in configuration file

前端 未结 3 813
礼貌的吻别
礼貌的吻别 2021-01-02 23:49

I\'m having trouble encrypting the database password in hibernate.cfg.xml

This is my property file.



        
3条回答
  •  攒了一身酷
    2021-01-03 00:32

    You might try this:

    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());
    

提交回复
热议问题