Watch your door-protect the data store (1)-sql Injection Defense

Source: Internet
Author: User
Tags sql injection attack sql injection defense

The first thing to declare is that this article is purely an ignorant view of a little developer without foresight and knowledge, and is intended only for reference in Web system security.

1. Common methods of SQL injection defense

An application that can connect to a database.
1. The data entered by the client is strictly protected;
2. Execute SQL statements using PreparedStatement;
3. Do not only validate at the page level, but also perform these validations synchronously at the service-side level;

2. Use regular expressions to mask special characters

Using a SQL injection attack is more of a special character up and down, such as "'", "*", "/", "–" and so on, if you use regular expressions to restrict special character input, these means will have no effect. The following code will block requests that contain special characters.

if (Pattern.matches ("\w+", name) ==false | | Pattern.matches ("\w+", pswd) ==false) {
Back to login interface
Request.setattribute ("Feedbackmsg", "User name and password not allowed to include special characters");
RequestDispatcher dispatcher = Request.getrequestdispatcher ("/web/page/login1.jsp?curr=0");
Dispatcher.forward (request, response);
Return
}

3, use PreparedStatement instead of statement

The SQL injection attack succeeds because new logic is added to the original SQL statement, and if you use PreparedStatement to execute the SQL statement and then just input parameters, the SQL injection attack will not work. This is because PreparedStatement does not allow the logical structure of the query to change at different insertion times. The sample code is as follows:

Connection Conn=null;
PreparedStatement Ps=null;
ResultSet Rs=null;

String sql= "SELECT COUNT (*) from usertable where name=? and password=? “;
Ps=conn.preparestatement (SQL);
Ps.setstring (1, name);
Ps.setstring (2, password);
Incorrect use of PreparedStatement

Using PreparedStatement to prevent SQL injection attacks is because "because PreparedStatement does not allow the logical structure of the query to change at different insertion times", this effect cannot be achieved if the following is used, because the SQL statements and parameters are not separated. They are grouped together and submitted at once.

Watch your door-protect the data store (1)-sql Injection Defense

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.