MyBatis防止SQL注入
public interface UserMapper{ String getNameByUserId(@Param("userId") String userId); }当我们传入userId="34;drop table user;"后
<select id="getNameByUserId" resultType="String"> SELECT name FROM user where id = #{userId} </select>select name from user where id = ?<select id="getNameByUserId" resultType="String"> SELECT name FROM user where id = ${userId} </select>select name from user where id = 34;drop table user;#{}是经过预编译的,是安全的;${}是未经过预编译的,仅仅是取变量的值,是非安全的,存在SQL注入。尽量采用“#{xxx}”这样的格式。若不得不使用“${xxx}”这样的参数,要手工地做好过滤工作,来防止sql注入攻击。涉及到动态表名和列名时,只能使用“${xxx}”这样的参数格式。所以,这样的参数需要我们在代码中手工进行处理来防止注入。

浙公网安备 33010602011771号