使用IBATIS防止sql注入
对于ibaits参数引用可以使用#和$两种写法,其中#写法会采用预编译方式,将转义交给了数据库,不会出现注入问题;如果采用$写法,则相当于拼接字符串,会出现注入问题。
例如,如果属性值为“' or '1'='1 ”,采用#写法没有问题,采用$写法就会有问题。
对于like语句,难免要使用$写法,
1. 对于Oracle可以通过'%'||'#param#'||'%'避免;
2. 对于MySQL可以通过CONCAT('%',#param#,'%')避免;
3. MSSQL中通过'%'+#param#+'% 。
4. #与$区别:#xxx# 代表xxx是属性值,map里面的key或者是你的pojo对象里面的属性, ibatis会自动在它的外面加上引号,表现在sql语句是这样的 where xxx = 'xxx' ;
$xxx$ 则是把xxx作为字符串拼接到你的sql语句中, 比如 order by topicId , 语句这样写 ... order by #xxx# ibatis 就会把他翻译成 order by 'topicId' (这样就会报错) 语句这样写 ... order by $xxx$ ibatis 就会把他翻译成 order by topicId
为了防止SQL注入,iBatis模糊查询时也要避免使用$$来进行传值。下面是三个不同数据库的ibatis的模糊查询传值。
- mysql: select * from stu where name like concat('%',#name #,'%')
- oracle: select * from stu where name like '%'||#name #||'%'
- SQL Server:select * from stu where name like '%'+#name #+'%
如:
- <!-- 用途:小二后台查询活动的数目 -->
- <select id="countActivitySearch" resultclass="java.lang.Long" parameterclass="actDO">
- <![CDATA[
- select count(id) from activity
- ]]>
- <dynamic prepend="WHERE">
- <isNotNull prepend=" AND " property="name">
- name LIKE CONCAT('%', #name#, '%')
- </isNotNull>
- <isNotNull prepend=" AND " property="itemId">
- itemId = #itemId#
- </isNotNull>
- <isNotNull prepend=" AND " property="itemName">
- itemName LIKE CONCAT('%', #itemName#, '%')
- </isNotNull>
- <isNotNull prepend=" AND " property="status">
- status = #status#
- </isNotNull>
- <isNotNull prepend=" AND " property="actStatus">
- actStatus = #actStatus#
- </isNotNull>
- <isNotNull prepend=" AND " property="domain">
- domain LIKE CONCAT('%', #domain#, '%')
- </isNotNull>
- </dynamic>
- </select>
- <!-- 用途:小二后台查询活动的列表 -->
- <select id="searchActivityForList" resultMap="actResult" parameterclass="actDO">
- <![CDATA[
- select * from activity
- ]]>
- <dynamic prepend="WHERE">
- <isNotNull prepend=" AND " property="name">
- name LIKE CONCAT('%', #name#, '%')
- </isNotNull>
- <isNotNull prepend=" AND " property="itemId">
- itemId = #itemId#
- </isNotNull>
- <isNotNull prepend=" AND " property="itemName">
- itemName LIKE CONCAT('%', #itemName#, '%')
- </isNotNull>
- <isNotNull prepend=" AND " property="status">
- status = #status#
- </isNotNull>
- <isNotNull prepend=" AND " property="actStatus">
- actStatus = #actStatus#
- </isNotNull>
- <isNotNull prepend=" AND " property="domain">
- domain LIKE CONCAT('%', #domain#, '%')
- </isNotNull>
- </dynamic>
- <![CDATA[
- order by starttime desc, createtime desc
- limit
- #startRow#, #perPageSize#
- ]]>
- </select>
不要这样来写:
- <select id="searchActivityForCount" resultclass="java.lang.Long" >
- <![CDATA[
- select count(*) from activity
- ]]>
- <dynamic prepend="WHERE">
- <isNotNull prepend=" AND " property="name">
- name LIKE '%$name$%'
- </isNotNull>
- <isNotNull prepend=" AND " property="itemId">
- itemId LIKE '%$itemId$%'
- </isNotNull>
- <isNotNull prepend=" AND " property="itemName">
- itemName LIKE '%$itemName$%'
- </isNotNull>
- <isNotNull prepend=" AND " property="status">
- status = #status#
- </isNotNull>
- <isNotNull prepend=" AND " property="actStatus">
- actStatus = #actStatus#
- </isNotNull>
- <isNotNull prepend=" AND " property="domain">
- domain LIKE '%$domain$%'
- </isNotNull>
- </dynamic>
- </select>
- <select id="searchActivityForList" resultMap="actResult" parameterclass="actDO">
- <![CDATA[
- select * from activity
- ]]>
- <dynamic prepend="WHERE">
- <isNotNull prepend=" AND " property="name">
- name LIKE '%$name$%'
- </isNotNull>
- <isNotNull prepend=" AND " property="itemId">
- itemId LIKE '%$itemId$%'
- </isNotNull>
- <isNotNull prepend=" AND " property="itemName">
- itemName LIKE '%$itemName$%'
- </isNotNull>
- <isNotNull prepend=" AND " property="status">
- status = #status#
- </isNotNull>
- <isNotNull prepend=" AND " property="actStatus">
- actStatus = #actStatus#
- </isNotNull>
- <isNotNull prepend=" AND " property="domain">
- domain LIKE '%$domain$%'
- </isNotNull>
- </dynamic>
- <![CDATA[
- order by starttime desc, createtime desc
- limit
- #startRow#, #perPageSize#
- ]]>
- </select>