避免sql注入相干的一些过程:
1,mybatis解析sql文件里的#–>2,jdbc预编译时与数据库交互–>3,jdbc设置值时与数据库交互
一,mybatis将#符号解析成占位符’?’
先看对$符号:
public String handleToken(String content) {
Object parameter = context.getBindings().get("_parameter");
if (parameter == null) {
context.getBindings().put("value", null);
} else if (SimpleTypeRegistry.isSimpleType(parameter.getClass())) {
context.getBindings().put("value", parameter);
}
Object value = OgnlCache.getValue(content, context.getBindings());
return (value == null ? "" : String.valueOf(value)); // issue #274 return "" instead of "null"
}
再看看#符号:
public String handleToken(String content) {
parameterMappings.add(buildParameterMapping(content));
return "?";//#符号则会返回占位符?
}
二,PreparedStatement设置参数时的解决
mybatis底层仍依赖的是jdbc,咱们再来看看com.mysql.jdbc.PreparedStatement在设置参数时的逻辑:
public synchronized void setString(int parameterIndex, String x) throws SQLException {
if (x == null) {
this.setNull(parameterIndex, 1);
} else {
this.checkClosed();
int stringLength = x.length();
StringBuffer buf;
//判断是否须要转译
if (this.connection.isNoBackslashEscapesSet()) {
boolean needsHexEscape = this.isEscapeNeededForString(x, stringLength);
Object parameterAsBytes;
byte[] parameterAsBytes;
if (!needsHexEscape) {
parameterAsBytes = null;
buf = new StringBuffer(x.length() + 2);
//在参数前后加单引号
buf.append('\'');
buf.append(x);
buf.append('\'');
......
......
String parameterAsString = x;
boolean needsQuoted = true;
if (this.isLoadDataQuery || this.isEscapeNeededForString(x, stringLength)) {//须要进行转译
needsQuoted = false;
buf = new StringBuffer((int)((double)x.length() * 1.1D));
buf.append('\'');//拼接单引号开始
for(int i = 0; i < stringLength; ++i) {
char c = x.charAt(i);
switch(c) {
case '\u0000':
buf.append('\\');
buf.append('0');
break;
case '\n':
buf.append('\\');
buf.append('n');
break;
case '\r':
buf.append('\\');
buf.append('r');
break;
case '\u001a':
buf.append('\\');
buf.append('Z');
break;
case '"':
if (this.usingAnsiMode) {
buf.append('\\');
}
buf.append('"');
break;
case '\'':
buf.append('\\');
buf.append('\'');
break;
case '\\':
buf.append('\\');
buf.append('\\');
break;
case '¥':
case '₩':
if (this.charsetEncoder != null) {
CharBuffer cbuf = CharBuffer.allocate(1);
ByteBuffer bbuf = ByteBuffer.allocate(1);
cbuf.put(c);
cbuf.position(0);
this.charsetEncoder.encode(cbuf, bbuf, true);
if (bbuf.get(0) == 92) {
buf.append('\\');
}
}
default:
buf.append(c);
}
}
buf.append('\'');//拼接单引号完结
parameterAsString = buf.toString();
}
......
setInt办法也贴一下:
public void setInt(int parameterIndex, int x) throws SQLException {
this.setInternal(parameterIndex, String.valueOf(x));
this.parameterTypes[parameterIndex - 1 + this.getParameterIndexOffset()] = 4;
}
setString办法中会判断是否须要转译,规范是看字符串参数里是否有\u0000,\n,\r,\u001a,”,’,\这些字符。
网上说参数前拼接引号还是有点情理的,不过这个引号是在mysql-java-connector这个jar里PreparedStatement对象中实现的
三,数据库预编译性能
如果数据库开启了预编译性能,数据库会将须要预编译的sql语句(含有占位符?)进行预编译存储,等到接管到参数时,简略地将参数替换掉占位符,这个时候参数不会再影响已编译后的sql语句(知乎里对于参数化sql查问避免sql注入的探讨)—不过感觉这些剖析有点想当然,但又不好反驳,次要是不太明确上面几点:
- mysql/jdbc里的预编译是否与数据库层面的预编译是一回事?
- 数据库层面的预编译与设置占位符的值时是怎么的过程?
相干的一些文章:
SQL预编译
Java JDBC下执行SQL的不同形式、参数化预编译进攻
预编译的两种形式
发表回复