Home > Java > javaTutorial > Java Mybatis prevents sql injection analysis

Java Mybatis prevents sql injection analysis

巴扎黑
Release: 2017-08-09 17:56:25
Original
1614 people have browsed it

This article introduces to you the relevant information of Mybatis to prevent sql injection through examples. It is very good and has reference value. Friends in need can refer to it.

SQL injection is familiar to everyone and is a common method. The attack method is that the attacker enters some strange SQL fragments in the form information or URL of the interface, such as statements such as "or '1'='1'", which may invade applications with insufficient parameter verification. Therefore, we need to do some work in our applications to prevent such attacks. In some highly secure applications, such as banking software, the method of replacing all SQL statements with stored procedures is often used to prevent SQL injection. This is of course a very safe method, but in our daily development, it may There is no need for such a rigid approach.

As a semi-automated persistence layer framework, the mybatis framework requires us to manually write its SQL statements ourselves. At this time, of course, we need to prevent SQL injection. In fact, Mybatis's sql is a structure with "input + output" function, similar to a function, as follows:


<select id="getBlogById" resultType="Blog" parameterType=”int”>
  select id,title,author,content
  from blog where id=#{id}
 </select>
Copy after login

Here, parameterType indicates the input parameter type, and resultType indicates Indicates the output parameter type. In response to the above, if we want to prevent sql injection, it is natural for us to work hard on input parameters. The highlighted part in the above code is the part where the input parameters are spliced ​​in sql. After passing in the parameters, print out the executed sql statement, and you will see that the sql looks like this:


select id,title,author,content from blog where id = ?
Copy after login

No matter what parameters are entered, the sql printed out will be like this. This is because mybatis has enabled the pre-compilation function. Before the sql is executed, the above sql will be sent to the database for compilation. During execution, the compiled sql will be used directly and the placeholder "?" will be replaced. Because sql injection can only affect the compilation process, this method can well avoid the problem of sql injection.

How does mybatis achieve sql pre-compilation? In fact, at the bottom of the framework, the PreparedStatement class in jdbc is at work. PreparedStatement is a subclass of Statement that we are very familiar with, and its object contains compiled SQL statements. This "ready" approach not only improves security, but also improves efficiency when executing a SQL multiple times. The reason is that the SQL has been compiled and does not need to be compiled again when executed again.

Having said that, can we definitely prevent SQL injection by using mybatis? Of course not, please look at the following code:


<select id="orderBlog" resultType="Blog" parameterType=”map”>
  select id,title,author,content
  from blog order by ${orderParam}
 </select>
Copy after login

Look carefully, the format of the inline parameter has changed from "#{xxx}" to ${xxx}. If we assign the value of "id" to the parameter "orderParam" and print out the sql, it will look like this:

select id,title,author,content from blog order by id

Obviously, this is the case There is no way to prevent sql injection. In mybatis, parameters in the format of "${xxx}" will directly participate in SQL compilation, so injection attacks cannot be avoided. But when it comes to dynamic table names and column names, only parameter formats such as "${xxx}" can be used. Therefore, such parameters need to be processed manually in the code to prevent injection.

Conclusion: When writing the mapping statement of mybatis, try to use the format of "#{xxx}". If you have to use parameters such as "${xxx}", you must manually filter them to prevent SQL injection attacks.

The above is the detailed content of Java Mybatis prevents sql injection analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template