JDBC的SQL注入漏洞分析和解决
2023-09-14 09:02:02 时间
SQL注入漏洞分析
SQL注入漏洞解决
需要采用PreparedStatement对象解决SQL注入漏洞。这个对象将SQL预先进行编译,使用?作为占位符。?所代表内容是SQL所固定。再次传入变量(包含SQL的关键字)。这个时候也不会识别这些关键字。
public class UserDao {
public boolean login(String username,String password){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
// 定义一个变量:
boolean flag = false;
try{
// 获得连接:
conn = JDBCUtils.getConnection();
// 编写SQL语句:
String sql = "select * from user where username = ? and password = ?";
// 预编译SQL
pstmt = conn.prepareStatement(sql);
// 设置参数:
pstmt.setString(1, username);
pstmt.setString(2, password);
// 执行SQL语句:
rs = pstmt.executeQuery();
if(rs.next()){
// 说明根据用户名和密码可以查询到这条记录
flag = true;
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release(rs, pstmt, conn);
}
return flag;
}
相关文章
- 代码审计-MetInfo 6.0.0 sql注入漏洞
- 如何使用PDO查询Mysql来避免SQL注入风险?ThinkPHP 3.1中的SQL注入漏洞分析!
- DT6.0关于SQL注入漏洞修复问题
- 安全测试全面总结-5-shell命令注入漏洞
- 安全测试全面总结-5-shell命令注入漏洞
- Vulhub漏洞靶场搭建和使用
- github仓库上的漏洞修复
- WEB漏洞攻防 - SQL注入 - Dnslog注入
- WEB漏洞攻防 -根据不同数据库类型之间的差异性进行注入
- CRLF Injection漏洞的利用与实例分析
- 一个由"2020年1月7日 京东出现的重大 Bug 漏洞"引起的思考...
- 通杀! 熬夜码的 - 八万字 - 让你一文读懂SQL注入漏洞原理及各种场景利用
- WEB漏洞攻防- SQL注入原理、判定方式、过滤及修复
- WEB漏洞攻防 - SQL注入 - 高权限注入
- 预处理prepareStatement是怎么防止sql注入漏洞的?
- Web应用程序漏洞-X-Forwarded-For注入
- CVE漏洞复现-CVE-2020-16846SaltStack命令注入