简体   繁体   English

提供MySQL JDBC驱动程序密码的最佳实践

[英]Best practice to provide MySQL JDBC driver password

I have JSP web application deployed in Tomcat7 server that use MySQL as a database. 我在Tomcat7服务器中部署了JSP Web应用程序,该应用程序使用MySQL作为数据库。 In database connection I'm currently providing clear text username and password as below. 在数据库连接中,我目前提供明文的用户名和密码,如下所示。

Connection conn = DriverManager.getConnection("jdbc:mysql://hostname:3306/schema", "user", "password");

As a security requirement I cannot use plain text passwords in my code and I cannot use configuration files to store information ether so everything has to be in a database somewhere. 作为安全性要求,我不能在代码中使用纯文本密码,也不能使用配置文件存储信息,因此所有内容都必须存储在某个地方的数据库中。

Q1 As for this issue can I provide a hashed input for password field in connection string.? Q1关于此问题,我可以在连接字符串中为密码字段提供哈希输入吗?

Q2 If I can't use file based configuration method which would be the best way to store sensitive information.? Q2如果我不能使用基于文件的配置方法,那将是存储敏感信息的最佳方法。

The best approach is to define JDBC datastore in tomcat and then look it up in your web application. 最好的方法是在tomcat中定义JDBC数据存储,然后在您的Web应用程序中查找它。 Following samples are shamelessly copied from tomcat documentation . 以下示例是从tomcat 文档中无耻复制的。 This way you do not have any passwords in your applicaion. 这样,您的应用程序中将没有任何密码。 The configuration is done by tomcat administrator, not by web application developer. 该配置由tomcat管理员完成,而不是由Web应用程序开发人员完成。

Context: 语境:

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
           maxActive="100" maxIdle="30" maxWait="10000"
           username="javauser" password="javadude" 
           driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/javatest"/>

web.xml web.xml中

<resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/TestDB</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

test.jsp test.jsp的

<sql:query var="rs" dataSource="jdbc/TestDB">
   select id, foo, bar from testdata
</sql:query>

servlet 的servlet

Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");
Connection conn = ds.getConnection();

Regarding your security requirements: this is harder, because the password is plaintext there as well. 关于您的安全要求:这比较难,因为那里的密码也是纯文本的。 You can limit access rights so only web container can read it. 您可以限制访问权限,以便只有Web容器才能读取它。 Encrypting password will not work with symmetrical ciphers because attacker can get them as well. 加密密码不适用于对称密码,因为攻击者也可以获取它们。 And asymmetrical ciphers - he can get decode key too. 非对称密码-他也可以获取解码密钥。 So you must set up the environment that attacker will not see content of the configuration files. 因此,您必须设置一个环境,使攻击者无法看到配置文件的内容。 If he is root, everything is lost anyway. 如果他是根,反正一切都会丢失。

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

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