SQL注入与防范(PreparedStatement的优点)----JDBC-3
来源:互联网 发布:nginx 域名指向目录 编辑:程序博客网 时间:2024/04/30 16:19
有些人学编程想必一定是受了黑客的影响,看着他们如何牛逼~自己也想向他们一样,黑了别人的网站什么的。
今天说的SQL注入虽然不应定能让你黑了某个网站,或许会让你明白一些黑客入侵的方法,提高自己的防范意识!
/** * SQL 注入小例 *操作oracle中一个user表,表中只有一个数据:username = Tom,password = 123456 *模拟一种有漏洞的登录方式 */@Testpublic void testSQLInjection() {//正常的用户名和密码,可以登录//String username = "Tom";//String password = "123456";//SQL注入,添加恶意代码非法登录!!!String username = "x' or password = ";String password = "or 'x' = 'x";//查询语句String sql = "select * from users where username = '"+username+"' and password = '"+password+"'";System.out.println(sql);//打印查询语句Connection connection = null;Statement statement = null;ResultSet result = null;try {//获取数据库连接,具体方法可看往期博文connection = getConnection();statement = connection.createStatement();result = statement.executeQuery(sql);//如果数据匹配则表明登录成功if(result.next()) {System.out.println("登录成功!");}else {System.out.println("登录失败,请检查用户名及密码!");}} catch(Exception e) {e.printStackTrace();} finally {//释放相应的资源if(result != null) {try {result.close();}catch (Exception e) {e.printStackTrace();}}if(statement != null) {try {statement.close();}catch (Exception e) {e.printStackTrace();}}if(connection != null) {try {connection.close();}catch (Exception e) {e.printStackTrace();}}}}分析:为什么username = ‘x' or password = '和password = ' or 'x' = 'x'也可以登录成功?很简单我们看看sql语句就知道了
select * from users where username = 'x' or password = ' and password = 'or 'x' = 'x',学过SQL的都知道,
这已经把原来的语句改变,是的语句永远为真,故而可以登录成功。那么有没有什么方法的方法呢?
下面我就来说一说解决办法:用PreparedStatement来替代Statement
示例如下:
/** * 使用 PreparedStatement 将有效的解决 SQL 注入问题. * 这也是PreparedStatement相较于Statement的一大优点 */@Testpublic void testSolutionSQLInjection() {//SQL注入,添加恶意代码妄图非法登录!!!String username = "x' or password = ";String password = "or 'x' = 'x";//查询语句String sql = "select * from users where username = ? and password = ?";System.out.println(sql);Connection connection = null;PreparedStatement preparedStatement = null;ResultSet result = null;try {//连接数据库,具体方法可看往期博文connection = getConnection();//使用PreparedStatementpreparedStatement = connection.prepareStatement(sql);preparedStatement.setString(1, username);preparedStatement.setString(2, password);result = preparedStatement.executeQuery();if(result.next()) {System.out.println("登录成功!");}else {System.out.println("登录失败,请检查用户名及密码!");}} catch(Exception e) {e.printStackTrace();} finally {//释放相应的资源if(result != null) {try {result.close();}catch (Exception e) {e.printStackTrace();}}if(PreparedStatement != null) {try {PreparedStatement.close();}catch (Exception e) {e.printStackTrace();}}if(connection != null) {try {connection.close();}catch (Exception e) {e.printStackTrace();}}<span style="white-space:pre"></span>}}
哈哈!结果当然是失败咯~
如有不足请多多指教如果对你有帮助请点赞支持
1 0
- SQL注入与防范(PreparedStatement的优点)----JDBC-3
- 在Eclipse中测试MySQL-JDBC(8)sql注入的防范PreparedStatement(处理命令)
- JDBC(四)SQL 注入与 PreparedStatement
- JDBC(PreparedStatement,sql注入)
- 用Statements和PreparedStatement防范sql注入
- jdbc防止sql注入-PreparedStatement
- jdbc防止sql注入-PreparedStatement
- SQL注入与防范
- SQL注入与防范
- SQL注入攻击的原理与防范
- 在jdbc中,preparedStatement是如何防止SQL注入的
- JDBC要点总结、SQL注入示例(Statement和PreparedStatement)
- SQL注入(PreparedStatement)
- JDBC:使用PreparedStatement防止SQL注入
- JDBC之SQL注入,PreparedStatement和Statement
- JDBC PreparedStatement 防止sql注入原理
- JDBC:使用PreparedStatement防止SQL注入
- SQL注入与PreparedStatement接口
- 掌握设计模式
- 师父
- LeetCode 21:Merge Two Sorted Lists
- Android应用架构
- Qt中网络编程(网络接口,TCP,UDP)
- SQL注入与防范(PreparedStatement的优点)----JDBC-3
- hw员工培养计划
- SlidingMenu-Fragment
- STL算法库-修改序列式操作(一)
- OI? OI!
- 设计模式中类的关系
- GUI 简单加法计算器模拟 监听事件获取其他域的属性值 窗口关闭的监听事件
- css
- react-native源码分析系列五 绘制js组件