如何保持与 Oracle 的 java JDBC 连接安全
How to keep java JDBC connection to oracle secured
我正在使用 java 构建 Web 应用程序并使用 JDBC 驱动程序
如果我错了请告诉我,我不认为使用这个代码块是安全的
我应该如何在不使用 "HARD CODED" 密码
的情况下使其安全
Connection connection = null;
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:mkyong","username","password");
connection.close();
取自:
http://www.mkyong.com/jdbc/connect-to-oracle-db-via-jdbc-driver-java/
例如,如果您指的是 Tomcat 网络应用程序,Java 代码将在服务器上执行。所以这样应该没问题。
但是如果你想有更多的安全性你可以创建一个credentials.properties文件,tomcat作为所有者和600的权利。然后你阅读该文件中的用户名和密码。
当然你可以在文件中加密它。
编辑:并且 credentials.properties 文件不应通过网络访问;)
试试 Jasypt,它应该有帮助:
http://jasypt.org/
示例(使用 .properties 文件机制):
第 1 步:找出加密密码并记下以备使用:
StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor();
字符串加密密码 = passwordEncryptor.encryptPassword(用户密码);
假设加密密码是:G6N718UuyPE5bHyWKyuLQSm02auQPUtm
第 2 步:使用加密密码将数据库连接凭据存储在 .properties 文件中
datasource.driver=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://localhost/reportsdb
datasource.username=报告用户
datasource.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)
Step3:我们如何读取这个值?像这样:
/*
* First, create (or ask some other component for) the adequate encryptor for
* decrypting the values in our .properties file.
*/
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword("jasypt"); // could be got from web, env variable...
/*
* Create our EncryptableProperties object and load it the usual way.
*/
Properties props = new EncryptableProperties(encryptor);
props.load(new FileInputStream("/path/to/my/configuration.properties"));
/*
* To get a non-encrypted value, we just get it with getProperty...
*/
String datasourceUsername = props.getProperty("datasource.username");
/*
* ...and to get an encrypted value, we do exactly the same. Decryption will
* be transparently performed behind the scenes.
*/
String datasourcePassword = props.getProperty("datasource.password");
// From now on, datasourcePassword equals "reports_passwd"...
为了解密加密值,我们只需使用 getProperty 访问它,就像任何其他非加密值一样。
如果您正在构建网络应用程序,您的资源应该在外部声明,例如在 Tomcat Context.xml 文件中。密码将位于此处,而不是您的代码中。保持安全是物理安全的问题。
我正在使用 java 构建 Web 应用程序并使用 JDBC 驱动程序
如果我错了请告诉我,我不认为使用这个代码块是安全的 我应该如何在不使用 "HARD CODED" 密码
的情况下使其安全Connection connection = null;
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:mkyong","username","password");
connection.close();
取自: http://www.mkyong.com/jdbc/connect-to-oracle-db-via-jdbc-driver-java/
Java 代码将在服务器上执行。所以这样应该没问题。
但是如果你想有更多的安全性你可以创建一个credentials.properties文件,tomcat作为所有者和600的权利。然后你阅读该文件中的用户名和密码。
当然你可以在文件中加密它。
编辑:并且 credentials.properties 文件不应通过网络访问;)
试试 Jasypt,它应该有帮助: http://jasypt.org/
示例(使用 .properties 文件机制):
第 1 步:找出加密密码并记下以备使用:
StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor();
字符串加密密码 = passwordEncryptor.encryptPassword(用户密码);
假设加密密码是:G6N718UuyPE5bHyWKyuLQSm02auQPUtm
第 2 步:使用加密密码将数据库连接凭据存储在 .properties 文件中 datasource.driver=com.mysql.jdbc.Driver datasource.url=jdbc:mysql://localhost/reportsdb datasource.username=报告用户 datasource.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)
Step3:我们如何读取这个值?像这样:
/*
* First, create (or ask some other component for) the adequate encryptor for
* decrypting the values in our .properties file.
*/
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword("jasypt"); // could be got from web, env variable...
/*
* Create our EncryptableProperties object and load it the usual way.
*/
Properties props = new EncryptableProperties(encryptor);
props.load(new FileInputStream("/path/to/my/configuration.properties"));
/*
* To get a non-encrypted value, we just get it with getProperty...
*/
String datasourceUsername = props.getProperty("datasource.username");
/*
* ...and to get an encrypted value, we do exactly the same. Decryption will
* be transparently performed behind the scenes.
*/
String datasourcePassword = props.getProperty("datasource.password");
// From now on, datasourcePassword equals "reports_passwd"...
为了解密加密值,我们只需使用 getProperty 访问它,就像任何其他非加密值一样。
如果您正在构建网络应用程序,您的资源应该在外部声明,例如在 Tomcat Context.xml 文件中。密码将位于此处,而不是您的代码中。保持安全是物理安全的问题。