简体   繁体   English

运行时的Hibernate数据库密码

[英]Hibernate database password at runtime

有没有办法在运行时请求数据库密码而不是将其(加密或不加密)放在hibernate.cfg.xml文件中?

Just about every configuration option in Hibernate has a corresponding method on the object being configured. 几乎Hibernate中的每个配置选项都在被配置的对象上有相应的方法。 In reality, the configuration is really just a way to bind XML to the objects being set up. 实际上,配置实际上只是将XML绑定到正在设置的对象的一种方法。 See this article for more information: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html 有关更多信息,请参阅此文章: http//docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html

That said, the onus is on you to collect the password at startup. 也就是说,您有责任在启动时收集密码。 That can be the most difficult part of the problem. 这可能是问题中最困难的部分。 Once you've collected the password, send it to the appropriate property. 收集密码后,将其发送到相应的属性。

Usually the best way to do it, if you're using a Java EE app server, is to use a JNDI look up to get the database connection instead of using a driver manager. 通常,如果您使用Java EE应用服务器,最好的方法是使用JNDI查找来获取数据库连接而不是使用驱动程序管理器。 That way the person who sets up the JNDI connection pool is the only one that has to know the password, and it's generally encrypted in the admin console so it's safe. 这样,设置JNDI连接池的人是唯一必须知道密码的人,并且它通常在管理控制台中加密,因此它是安全的。

我想如果您正在使用Hibernate配置的编程实例化,您可以从不包含密码的配置文件初始化它,在您要实例化的配置对象上设置数据库连接的附加属性,然后调用buildConfguration() 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM