我对使用Spring的JdbcTemplate编写类似下面代码的SQL注入存在疑虑:
jdbcTemplate.query("SELECT * FROM " + tableName, new TableMapper());
由于不能使用JdbcTemplate传递参数,因此需要通过连接来添加“tableName”变量。
只有管理员才能编辑“tableName”变量,但我想知道是否可以在此查询中进行SQL注入攻击。
否则,请描述一种安全的方法来完成这个操作?
我对使用Spring的JdbcTemplate编写类似下面代码的SQL注入存在疑虑:
jdbcTemplate.query("SELECT * FROM " + tableName, new TableMapper());
由于不能使用JdbcTemplate传递参数,因此需要通过连接来添加“tableName”变量。
只有管理员才能编辑“tableName”变量,但我想知道是否可以在此查询中进行SQL注入攻击。
否则,请描述一种安全的方法来完成这个操作?
String prepQuery = "INSERT INTO %s( name, dataid, groupid, uniqueid, type, filecontainer, filetype, fileurl, filepath, uploadstatus) VALUES (?,?,?,?,?,?,?,?,?,?)";
Object[] params = new Object[] {
name,
dataid,
groupid,
uniqueid,
type,
container,
filetype,
file_url,
path,
filePresent
};
int i = jdbcTemplate.update(prepQuery.formatted(dataid) ,params);
%s
。这里使用了一个名为 formatted
的实例版本的 String::format
。 String prepQuery = "INSERT INTO $tabname ( name, dataid, groupid, uniqueid, type, filecontainer, filetype, fileurl, filepath, uploadstatus) VALUES (?,?,?,?,?,?,?,?,?,?)";
Object[] params = new Object[] {
name,
dataid,
groupid,
uniqueid,
type,
container,
filetype,
file_url,
path,
filePresent
};
prepQuery = prepQuery.replace("$tableName",dataid);
int i = jdbcTemplate.update(prepQuery ,params);
是的,这是可能的。我建议您将代码修改为以下内容:
jdbcTemplate.query("SELECT * FROM ?", new Object[]{tableName},new TableMapper());
这个安全地将tableName插入到你的查询中;)
tableName
上进行了任何类型的清洁吗? - EM-Creations