当前位置:Gxlcms > 数据库问题 > mybatis模糊查询防止SQL注入

mybatis模糊查询防止SQL注入

时间:2021-07-01 10:21:17 帮助过:16人阅读

)

SQL:

    <select id="getInfo2" resultType="cn.xm.exam.bean.haul.Haulinfo"
        parameterType="hashmap">
        SELECT * FROM haulinfo
        <where>
            <if test="name != null">
                and bigname like %${name}%
            </if>
            <if test="status != null">
                and bigStatus = #{status}
            </if>
        </where>
    </select>

 

Java测试:

本来是模糊查询名字,结果对描述添加了过滤。

    @Test
    public void test2() throws SQLException {

        Map condition = new HashMap();
        condition.put("name", "% and bigdescription like 阳城");
        condition.put("status", "未开始");
        testMapper.getInfo2(condition);
    }

 

Preparing: SELECT * FROM haulinfo WHERE bigname like ‘%%‘ and bigdescription like ‘阳城%‘ and bigStatus = ? 

Parameters: 未开始(String)

Total: 2

 

2. bind + #{}  模糊查询 防止SQL注入 (#{}进行预编译,传递的参数不进行编译,只作为参数,相当于PreparedStatement)

bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文。比如:

<select id="selectBlogsLike" resultType="Blog">
  <bind name="pattern" value="% + _parameter.getTitle() + %" />
  SELECT * FROM BLOG
  WHERE title LIKE #{pattern}
</select>

 

 

 

SQL:

    <select id="getInfo" resultType="cn.xm.exam.bean.haul.Haulinfo"
        parameterType="hashmap">
        SELECT * FROM haulinfo
        <where>
            <if test="name != null">
                <bind name="names" value="%+name+%" />
                and bigname like #{names}
            </if>
            <if test="status != null">
                and bigStatus = #{status}
            </if>
        </where>
    </select>

 

Java测试:

    @Test
    public void test1() throws SQLException {

        Map condition = new HashMap();
        condition.put("name", "% and bigdescription like 阳城");
        condition.put("status", "未开始");
        testMapper.getInfo(condition);
    }

Preparing: SELECT * FROM haulinfo WHERE bigname like ? and bigStatus = ? 

Parameters: %%‘ and bigdescription like ‘阳城%(String), 未开始(String)

Total: 0

 

 

 

 

 

 

【结论】在编写MyBatis的映射语句时,尽量采用“#{xxx}”这样的格式。若不得不使用“${xxx}”这样的参数,要手工地做好过滤工作,来防止SQL注入攻击。

 

#{}:相当于JDBC中的PreparedStatement

${}:是输出变量的值

简单说,#{}是经过预编译的,是安全的${}是未经过预编译的,仅仅是取变量的值,是非安全的,存在SQL注入。

  如果我们order by语句后用了${},那么不做任何处理的时候是存在SQL注入危险的。你说怎么防止,那我只能悲惨的告诉你,你得手动处理过滤一下输入的内容。如判断一下输入的参数的长度是否正常(注入语句一般很长),更精确的过滤则可以查询一下输入的参数是否在预期的参数集合中。

 

mybatis模糊查询防止SQL注入

标签:mapper   存在   select   上下   表达式   name   方式   处理   防止   

人气教程排行