简体   繁体   English

Mybatis-防止在LIKE查询中再次进行SQL注入

[英]Mybatis - preventing agains SQL Injection in LIKE query

I am using sql server . 我正在使用sql server Lets look following query in mybatis : 让我们在mybatis查看以下查询:
param is type of string, in database SomeColumn is type of VARCHAR param是字符串类型,在数据库中SomeColumnVARCHAR类型

...
 = SomeColumn LIKE '%#{param, jdbcType=VARCHAR}%'  

It returns error: 它返回错误:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='param', mode=IN, javaType=class java.lang.Object, jdbcType=VARCHAR, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. org.mybatis.spring.MyBatisSystemException:嵌套异常为org.apache.ibatis.type.TypeException:无法设置映射参数:ParameterMapping {property ='param',mode = IN,javaType = class java.lang.Object,jdbcType = VARCHAR,numericalScale = null,resultMapId ='null',jdbcTypeName ='null',expression ='null'}。 Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . 原因:org.apache.ibatis.type.TypeException:使用JdbcType null为参数#1设置非null时出错。 Try setting a different JdbcType for this parameter or a different configuration property. 尝试为此参数或其他配置属性设置不同的JdbcType。 Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType VARCHAR . 原因:org.apache.ibatis.type.TypeException:使用JdbcType VARCHAR为参数#1设置非null时出错。 Try setting a different JdbcType for this parameter or a different configuration property. 尝试为此参数或其他配置属性设置不同的JdbcType。 Cause: com.microsoft.sqlserver.jdbc.SQLServerException: The index 1 is out of range. 原因:com.microsoft.sqlserver.jdbc.SQLServerException:索引1超出范围。

I can't deal with it (any ideas?), but I find something: 我无法处理(任何想法?),但是我发现了一些东西:

...    
<bind name="param" value="'%' + param + '%'" />
...
     = SomeColumn LIKE #{param}    

And it does work. 它确实有效。 I think that prevent me agains SQL Injection but I am not sure. 我认为这阻止了我的SQL Injection但我不确定。 Can you answer to it ? 你能回答吗? Is it SQL Injection safe ? SQL Injection安全吗?

We faced the same issue and the below method worked for us: 我们遇到了同样的问题,以下方法对我们有效:

eg select * from TABLE1 where COLUMN1 like '%' || 例如,从TABLE1中选择*,其中COLUMN1如'%'||。 #{PARAM_NAME} || #{PARAM_NAME} || '%'; '%';

This prevents SQL Injection and allows to use # in LIKE query. 这样可以防止SQL注入,并允许在LIKE查询中使用#。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM