以编程方式使用动态用户名和密码配置Hibernate [英] Programmatically configure Hibernate with dynamic username and password
本文介绍了以编程方式使用动态用户名和密码配置Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在实现一个Web应用程序,用户需要使用他的数据库用户名和密码进行身份验证。
我想要使用相同的输入用户名和密码连接到数据库。
换句话说,我想这两个字段:(dbusername和hibernate配置文件中的dbpassword):
< property name =hibernate.connection.username> dbusername&属性>
< property name =hibernate.connection.password> dbpassword< / property>
可以填充dynimacally,取决于输入用户名和密码登录Web应用程序的用户。
是这个功能吗?
感谢
配置cfg = new Configuration();
cfg.configure(hibernate.cfg.xml); // hibernate config xml文件名
String newUserName,newPassword; //根据需要设置它们
cfg.getProperties()。setProperty(hibernate.connection.password,newPassword);
cfg.getProperties()。setProperty(hibernate.connection.username,newUserName);
sessionFactory = cfg.buildSessionFactory();
I am implementing a web application where the user needs to authenticate with his database username and password. I woudld like to use the same entered username and password to connect to the database.
In other words, i would like that these two fields:(dbusername and dbpassword) in the hibernate configuration file:
<property name="hibernate.connection.username">dbusername</property>
<property name="hibernate.connection.password">dbpassword</property>
can be filled dynimacally dependant on the user who enterd his username and password to log in the web application.
is this do-able?
thanks
解决方案
Here is how you can acheive
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml"); //hibernate config xml file name
String newUserName,newPassword;//set them as per your needs
cfg.getProperties().setProperty("hibernate.connection.password",newPassword);
cfg.getProperties().setProperty("hibernate.connection.username",newUserName);
sessionFactory = cfg.buildSessionFactory();
这篇关于以编程方式使用动态用户名和密码配置Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文