Loading

mybatis防止sql注入

Mybatis框架下易产生SQL注入漏洞的情况主要分为以下三种:

1、模糊查询

Select * from news where title like%#{title}%

 

在这种情况下使用#程序会报错,新手程序员就把#号改成了$,这样如果java代码层面没有对用户输入的内容做处理势必会产生SQL注入漏洞。

正确写法:

select * from news where tile like concat(‘%’,#{title}, ‘%’)

 

2、in 之后的多个参数

in之后多个id查询时使用# 同样会报错,

Select * from news where id in (#{ids})

 

正确用法为使用foreach,而不是将#替换为$

id in
<foreach collection="ids" item="item" open="("separatosr="," close=")">
#{ids} 
</foreach>

3、order by 之后

用order by排序,其之后并不能有单引号,而预编译会直接拼接单引号' ,所以排序失败。导致order by 之后只能使用$符号。

 

解决方法:

1.使用PageHelper进行order by排序

2.过滤危险的字符

posted @ 2021-03-06 23:38  Atomovo  阅读(651)  评论(0编辑  收藏  举报