我有一个条件,按顺序和限制的查询。我正在使用准备好的语句来设置where条件和限制。目前,我正在使用字符串追加命令,从而导致SQL注入漏洞。
如果我这样做,则无法使用像这样的order by ? ?
SQL Order功能无法通过set字符串进行排序。
查询示例:
SELECT siteid, technology, address, state, status FROM archive LEFT OUTER
JOIN mappings ON siteid = child_site_id order by siteid asc limit ? offset ?
SELECT siteid, technology, address, state, status FROM archive LEFT OUTER
JOIN mappings ON siteid = child_site_id order by siteid asc limit 10 offset 0
我可以通过其他任何方式来避免SQL注入。
最佳答案
做这样的事情并将其连接起来:
List<String> allowedSortableColumns = Arrays.asList(new String[]{"siteid", "technology", "address"})
if(! allowedSortableColumns.contains(sortByColumn)){
throw new RuntimeException("Cannot sort by: " + sortByColumn);
}
// Continue here and it's safe to concatenate sortByColumn...
您可以进行消毒和其他操作,但这应在您的情况下起作用