SQL注入问题:
sql存在漏洞,会被攻击导致数据泄露 ==SQL会被拼接 or==
代码理解:
import com.kuang.lseeon02.utils.JdbcUtils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class SQL注入 { public static void main(String[] args) { // login("kuangshen", "123456"); 正常登录 login(" 'or'1 = 1", " 'or'1 = 1"); // SQL注入操作 } // 登陆业务 public static void login(String username, String password) { Connection conn= null; Statement st = null; ResultSet rs = null; try { conn = JdbcUtils.getConnection(); // 使用编写好的JdbcUtils类中的静态方法获取链接 st = conn.createStatement(); // 获得sql的执行对象 // select * from users where `NAME` = 'kuangshen' and `password` = '123456' // select * from users where `NAME` = ' ' or '1 = 1' and `password` = ' ' or '1 = 1'' String sql = "SELECT * FROM USERS WHERE `NAME` = '" + username + "'and`password`='" + password + "'"; rs = st.executeQuery(sql); // 执行sql语句,返回结果是一个结果集 while (rs.next()) { System.out.println(rs.getString("NAME")); System.out.println(rs.getString("password")); System.out.println("======================================="); } } catch (SQLException e) { throw new RuntimeException(e); } finally { JdbcUtils.release(conn, st, rs); // 使用编写好的JdbcUtils类中的静态方法释放资源 } } }
输出:
kuangshen
123456
=======================================
lisi
123456
=======================================
wangwu
123456
=======================================
可见 :sql存在漏洞,会被攻击导致数据泄露 ==SQL会被拼接 or==