JdbcTemplate.queryForList(sql)产生的漏洞问题修复
1、在一些遗留项目中使用的是JdbcTemplate.queryForList(sql)语句来查询数据,JdbcTemplate是Spring框架封装的数据库查询工具类,连接池也是安全的。但是在持续集成的时候扫描出了漏洞,一查才发现是String sql 是使用的+=“”来拼接的。这样产生了sql注入的漏洞。
2、解决办法
String sql = "select * " +
"from student_info t " +
"where t.key =:key " +
"group by t.key";
//只需要在SQL语句后面加入
MapSqlParameterSource source=new MapSqlParameterSource();
source.addValue("key","参数");
NamedParameterJdbcTemplate template=new NamedParameterJdbcTemplate(jdbcTemplate);
List<Map<String,Object>> list = template.queryForList(sql,source);
//完成漏洞解决