`

使用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的模糊查询传值。

Sql代码 复制代码 收藏代码
  1. mysql: select * from stu where name like concat('%',#name #,'%')     
  2.     
  3. oracle: select * from stu where name like '%'||#name #||'%'    
  4.     
  5. SQL Server:select * from stu where name like '%'+#name #+'%     



如:

Sql代码 复制代码 收藏代码
  1. <!-- 用途:小二后台查询活动的数目 -->   
  2.        <select id="countActivitySearch" resultClass="java.lang.Long" parameterClass="actDO">   
  3.         <![CDATA[   
  4.             select count(id) from activity    
  5.             ]]>   
  6.            
  7.         <dynamic prepend="WHERE">   
  8.             <isNotNull prepend=" AND " property="name">   
  9.                 name LIKE CONCAT('%', #name#, '%')   
  10.             </isNotNull>   
  11.             <isNotNull prepend=" AND "  property="itemId">   
  12.                 itemId = #itemId#   
  13.             </isNotNull>   
  14.             <isNotNull prepend=" AND " property="itemName">   
  15.                 itemName LIKE CONCAT('%', #itemName#, '%')   
  16.             </isNotNull>            
  17.             <isNotNull prepend=" AND " property="status">   
  18.                 status = #status#   
  19.             </isNotNull>    
  20.             <isNotNull prepend=" AND " property="actStatus">   
  21.                 actStatus = #actStatus#   
  22.             </isNotNull>             
  23.             <isNotNull prepend=" AND " property="domain">   
  24.                 domain LIKE CONCAT('%', #domain#, '%')   
  25.             </isNotNull>   
  26.         </dynamic>   
  27.            
  28.     </select>   
  29.        
  30.     <!-- 用途:小二后台查询活动的列表 -->   
  31.  
  32.     <select id="searchActivityForList" resultMap="actResult" parameterClass="actDO">   
  33.         <![CDATA[   
  34.             select * from activity    
  35.         ]]>   
  36.            
  37.         <dynamic prepend="WHERE">   
  38.             <isNotNull prepend=" AND " property="name">   
  39.                 name LIKE CONCAT('%', #name#, '%')   
  40.             </isNotNull>   
  41.             <isNotNull prepend=" AND "  property="itemId">   
  42.                 itemId = #itemId#   
  43.             </isNotNull>   
  44.             <isNotNull prepend=" AND " property="itemName">   
  45.                 itemName LIKE CONCAT('%', #itemName#, '%')   
  46.             </isNotNull>            
  47.             <isNotNull prepend=" AND " property="status">   
  48.                 status = #status#   
  49.             </isNotNull>    
  50.             <isNotNull prepend=" AND " property="actStatus">   
  51.                 actStatus = #actStatus#   
  52.             </isNotNull>             
  53.             <isNotNull prepend=" AND " property="domain">   
  54.                 domain LIKE CONCAT('%', #domain#, '%')   
  55.             </isNotNull>   
  56.         </dynamic>   
  57.            
  58.         <![CDATA[   
  59.             order by starttime desc, createtime desc  
  60.             limit    
  61.                 #startRow#, #perPageSize#      
  62.         ]]>   
  63.     </select>  


不要这样来写:

Sql代码 复制代码 收藏代码
  1. <select id="searchActivityForCount" resultClass="java.lang.Long" >   
  2.         <![CDATA[   
  3.             select count(*) from activity   
  4.             ]]>   
  5.            
  6.         <dynamic prepend="WHERE">   
  7.             <isNotNull prepend=" AND " property="name">   
  8.                 name LIKE '%$name$%'  
  9.             </isNotNull>   
  10.             <isNotNull prepend=" AND " property="itemId">   
  11.                 itemId LIKE '%$itemId$%'  
  12.             </isNotNull>   
  13.             <isNotNull prepend=" AND " property="itemName">   
  14.                 itemName LIKE '%$itemName$%'  
  15.             </isNotNull>            
  16.             <isNotNull prepend=" AND " property="status">   
  17.                 status = #status#   
  18.             </isNotNull>    
  19.             <isNotNull prepend=" AND " property="actStatus">   
  20.                 actStatus = #actStatus#   
  21.             </isNotNull>             
  22.             <isNotNull prepend=" AND " property="domain">   
  23.                 domain LIKE '%$domain$%'  
  24.             </isNotNull>   
  25.         </dynamic>   
  26.     </select>   
  27.        
  28.     <select id="searchActivityForList" resultMap="actResult" parameterClass="actDO">   
  29.         <![CDATA[   
  30.             select * from activity    
  31.         ]]>   
  32.            
  33.         <dynamic prepend="WHERE">   
  34.             <isNotNull prepend=" AND " property="name">   
  35.                 name LIKE '%$name$%'  
  36.             </isNotNull>   
  37.             <isNotNull prepend=" AND " property="itemId">   
  38.                 itemId LIKE '%$itemId$%'  
  39.             </isNotNull>   
  40.             <isNotNull prepend=" AND " property="itemName">   
  41.                 itemName LIKE '%$itemName$%'  
  42.             </isNotNull>            
  43.             <isNotNull prepend=" AND " property="status">   
  44.                 status = #status#   
  45.             </isNotNull>    
  46.             <isNotNull prepend=" AND " property="actStatus">   
  47.                 actStatus = #actStatus#   
  48.             </isNotNull>             
  49.             <isNotNull prepend=" AND " property="domain">   
  50.                 domain LIKE '%$domain$%'  
  51.             </isNotNull>   
  52.         </dynamic>   
  53.            
  54.         <![CDATA[   
  55.             order by starttime desc, createtime desc  
  56.             limit    
  57.                 #startRow#, #perPageSize#      
  58.         ]]>   
  59.     </select>  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics