我担心在一些类似于此的代码中使用 spring 的 JdbcTemplate 进行 SQL 注入(inject):
jdbcTemplate.query("SELECT * FROM " + tableName, new TableMapper());
“tableName”变量是通过连接添加的,因为它不可能作为 JdbcTemplate 的参数传递。
“tableName”变量只能由管理员编辑,但我想知道在技术上是否可以对该查询进行 SQL 注入(inject)攻击。
否则你能描述一个安全的方法吗?
最佳答案
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);
这只是一个替代,但表名不能以动态方式传递。
引用 doc
关于sql - JdbcTemplate动态表名和SQL注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31557035/