使用Mybatis的@SelectProvide会不会导致注入攻击?

各位前辈,最近我在用mybatis注解开发,在使用动态sql的时候,是这样用的:

@SelectProvider(type = SqlProvider.class, method = "countByDate")
int countVoucherByDate(@Param("pre_date") String pre_date,
                       @Param("post_date") String post_date,
                       @Param("merchant_id") int merchant_id);

在SqlProvider类中的方法是:

public String countByDate(Map<String, Object> param){
    String pre_date = (String) param.get("pre_date");
    String post_date = (String) param.get("post_date");
    int merchant_id = (int) param.get("merchant_id");

    String sql = " select count(*) from voucher_t" +
            " where 1 = 1";
    if(!StringUtils.isEmpty(pre_date) && !StringUtils.isEmpty(post_date)){
        sql += " and create_date > " + "\"" + pre_date + "\"";
        sql += " and create_date < " + "\"" + post_date + " 23:59:59" + "\"";
    }
    sql +=  " and merchant_id = " + merchant_id;
    return sql;
}

如果用这种方法,会不会导致sql注入攻击?因为返回的是完整的sql执行语句,没有经过mybatis的preparedstatement处理。
如何改进?

阅读 7.4k
1 个回答
新手上路,请多包涵

这样是会导致注入攻击的,比如对最后一行的改动,使用 " and merchant_id = #{merchant_id} "代替" and merchant_id = " + merchant_id就会经过mybatis的preparedstatement处理了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题