findbugs和数据库密码安全问题

mab*_*zer 8 java security findbugs

我正在使用以下代码初始化数据库连接:


 public Connection getConnection() {
        try {
            if (null == connection) {
                String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
                Class.forName(driverName);

                // Create a connection to the database
                String serverName = "localhost";
                String database = "database";
                String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
                String username = "username";
                String password = "password";
                connection = DriverManager.getConnection(url, username, password);
            }
            return connection;
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
        throw new NullPointerException("Cannot establish database connection...");
    }
Run Code Online (Sandbox Code Playgroud)

我知道这样做是不好的做法,我也FindBugs反对代码,并且遇到了以下安全问题: This code creates a database connect using a hardcoded, constant password. Anyone with access to either the source code or the compiled code can easily learn the password.

在没有此安全漏洞的情况下初始化数据库连接的最佳方法是什么?

roo*_*ook 2

绝大多数 Web 应用程序都使用硬编码的用户名/密码进行 SQL 连接。将生产凭证检查到源代码管理中,或者赋予实习生删除生产数据库的能力,通常都会遭到反对。生产凭证应受到保护,只有特权员工才能访问它们。

Web 应用程序泄露其配置文件是很常见的。例如,如果 .xml 文件存储在 webroot 中,则可以远程访问它:http://localhost/configs/db_config.xml

常见的做法是禁止访问数据库(阻止 mysql 的 tcp 端口 3306)。事实上,这是 PCI-DSS 的要求。即使从哪里获得用户名和密码,也是没有用的。