[Java][SQL] 查询时硬编码用户名和密码

[Java][SQL] Hardcoded username and password on query

我的 java 代码有问题。我的大学老师让我使用 "kiuwan" 作为在线代码评估器,他在我的代码中发现了这个问题:

Hardcoded credentials (username / password) are visible to any person reading the source code. If the resource protected by such hardcoded credentials is important, this may compromise system security. With hardcoded credentials, change is difficult. If the target account is compromised, and the software is deployed in production, a code change is needed, which forces a redeployment. Please note that source code access is not always necessary: if an attacker has access to the JAR file, he/she may dis-assembly it to recover the password in clear.

它在我的 3 个查询中发现了这个问题:

public static final String CHECK_USER = "SELECT nome FROM utenti WHERE nome=? AND password=?";
public static final String INSERT_USER = "INSERT INTO utenti (nome, password) VALUES (?, ?)";
public static final String CHECK_USER_NAME = "SELECT nome FROM utenti WHERE nome=?";

我该如何解决?我以这种方式制作它们(从文本字段中获取信息)以进行登录并检查数据库。

谢谢大家!

我不认为你的老师在谈论这些问题。我认为他/她在谈论您的代码如何创建其数据库连接。他/她发现您已将数据库帐户 的用户名和密码硬连接到您的Java 代码中。

一个简单的解决方案是从配置文件中读取数据库帐户详细信息...尽管现在您遇到了保持配置文件安全的问题。

(我为什么这么想?因为你从老师那里引用的文字谈到从源代码或 JAR 文件中读取密码......而不是从数据库中读取密码。)


郑重声明,将密码存储在数据库中也是一个坏主意。但这不是你的老师所说的。

避免在数据库中存储密码的技术更加困难。一个典型的解决方案是为密码创建一个种子 crypto-hash,并将 that 存储在数据库中。但这意味着您无法恢复原始密码。如果您需要这样做,那么事情会变得更加 "hairy" ... 从安全角度来看。