javaee sql注入问题

news2024/9/23 3:33:43

jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="TestLoginServlet" method="post">
	<pre>
		账号:<input type="text" name="uname">
		密码:<input type="password" name="pwd">
		<input type="submit" name="sub" value="登录">
	</pre>
</form>

</body>
</html>

存在SQL注入问题的代码

package com.yyy.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * author:呆萌老师
 *  qq: 2398779723
 *  weixin: it_daimeng
 */
@WebServlet("/TestLoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//1.获得用户的输入
		String uname=request.getParameter("uname");
		String pwd=request.getParameter("pwd");
		//2.连接数据库查询
		try {
			Class.forName("com.mysql.jdbc.Driver");
			
			Connection connection=  DriverManager.getConnection("jdbc:mysql://localhost:3306/itstar?characterEncoding=utf-8","itstar","yyy123456");
			
			// uname=zhangsan' or '1'='1  // sql注入
			
		    //防止sql注入
			//String sql="select * from user where uname=? and upwd=?";			
			
			//预定义语句命令对象
//			PreparedStatement pstatement= connection.prepareStatement(sql);			
//		
//			pstatement.setString(1, uname);
//			
//			pstatement.setString(2, pwd);		
//			
//			ResultSet rSet= pstatement.executeQuery();
			
			Statement statement = connection.createStatement();
			String sql = "select * from user where uname = '"+uname+"' and upwd = '"+pwd+"'";
			//"select * from user where uname = 'zhangsan' or '1'='1' and pwd = '"+pwd+"'"
			ResultSet rSet = statement.executeQuery(sql);
			
			if(rSet.next())
				response.getWriter().println("<script>alert('登录成功')</script>");
			else
				response.getWriter().println("用户名或密码出错");
			
			rSet.close();
			
			connection.close();
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//3.处理结果
		
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

在这里插入图片描述
当输入的用户名为y1’ or ‘1’ = ‘1时,SQL语句会拼接为
“select * from user where uname = ‘y1’ or ‘1’=‘1’ and pwd = '”+pwd+"’"
因为or ‘1’='1’的存在,此时不管输入的密码是什么,都会登录成功。

避免sql注入的代码

package com.yyy.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * author:呆萌老师
 *  qq: 2398779723
 *  weixin: it_daimeng
 */
@WebServlet("/TestLoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//1.获得用户的输入
		String uname=request.getParameter("uname");
		String pwd=request.getParameter("pwd");
		//2.连接数据库查询
		try {
			Class.forName("com.mysql.jdbc.Driver");
			
			Connection connection=  DriverManager.getConnection("jdbc:mysql://localhost:3306/itstar?characterEncoding=utf-8","itstar","yyy123456");
			
			// uname=zhangsan' or '1'='1  // sql注入
			
		    //防止sql注入
			String sql="select * from user where uname=? and upwd=?";			
			
			//预定义语句命令对象
			PreparedStatement pstatement= connection.prepareStatement(sql);			
		
			pstatement.setString(1, uname);
			
			pstatement.setString(2, pwd);		
			
			ResultSet rSet= pstatement.executeQuery();
			
//			Statement statement = connection.createStatement();
//			String sql = "select * from user where uname = '"+uname+"' and upwd = '"+pwd+"'";
//			//"select * from user where uname = 'zhangsan' or '1'='1' and pwd = '"+pwd+"'"
//			ResultSet rSet = statement.executeQuery(sql);
			
			if(rSet.next())
				response.getWriter().println("<script>alert('登录成功')</script>");
			else
				response.getWriter().println("用户名或密码出错");
			
			rSet.close();
			
			connection.close();
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//3.处理结果
		
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

在这里插入图片描述
使用预定义sql语句,此时不管页面输入的参数是什么,都会被加上引号放到sql中作为参数值。这样就可以避免sql注入的bug了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/673355.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

第三章 GoogLeNet网络详解

系列文章目录 第一章 AlexNet网络详解 第二章 VGG网络详解 第三章 GoogLeNet网络详解 第四章 ResNet网络详解 第五章 ResNeXt网络详解 第六章 MobileNetv1网络详解 第七章 MobileNetv2网络详解 第八章 MobileNetv3网络详解 第九章 ShuffleNetv1网络详解 第十章…

基于Java软件科技公司信息管理系统设计实现(源码+lw+部署文档+讲解等)

博主介绍&#xff1a; ✌全网粉丝30W,csdn特邀作者、博客专家、CSDN新星计划导师、java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战 ✌ &#x1f345; 文末获取源码联系 &#x1f345; &#x1f447;&#x1f3fb; 精…

STM32启动模式

M3/M4/M7内核复位后&#xff0c;做的第一件事&#xff1a; 从地址0x00000000处取出堆栈指针MSP的初始值&#xff0c;该值就是栈顶地址。从地址0x00000004处取出程序计数器指针PC的初始值&#xff0c;该值是复位向量。 芯片厂商可能会把0x00000000和0x00000004地址映射到其它的…

ADC(模数转换)详解

ADC&#xff08;模数转换&#xff09;详解 前言ADC的定义ADC简介ADC特性ADC时钟工作模式单通道单次转换练习多通道扫描模式单次转换 前言 在STM32微控制器中&#xff0c;ADC代表模数转换器&#xff08;Analog-to-Digital Converter&#xff09;。ADC是一种用于将模拟信号转换为…

【Linux】程序地址空间?进程地址空间

目录 程序地址空间回顾进程地址空间什么是进程地址空间&#xff1f;进程地址空间与PCB、物理内存、页表和磁盘之间的关系为什么要存在虚拟地址空间&#xff1f;重新理解地址空间 程序地址空间回顾 了解进程的运行&#xff1a; 1 #include <stdio.h>2 #include <unist…

Web安全——HTML基础

HTML 一、对于前端以及后端的认识以及分析二、HTML认知1、网页的组成2、浏览器3、Web标准 三、简单的HTML页面架构四、HTML常见标签1、meta标签2、标题标签3、文本属性4、form表单5、a 标签6、锚文本7、img 标签8、table 表格9、列表标签9.1、无序列表9.2、有序列表 10、框架的…

个人工作总结和计划怎么写

工作总结和计划怎么写1 20__年就快结束&#xff0c;回首年的工作&#xff0c;有硕果累累的喜悦&#xff0c;有与同事协同攻关的艰辛&#xff0c;也有遇到困难和挫折时惆怅&#xff0c;时光过得飞快&#xff0c;不知不觉中&#xff0c;充满希望的_年就伴随着新年伊始即将临近。可…

C++ 重载函数

文章目录 前言一、什么是重载函数&#xff1f;二、重载函数的类型&#xff0c;作用。1. 构造函数重载&#xff1a;2. 运算符重载&#xff1a;3. 函数重载&#xff1a;4. 成员函数重载&#xff1a; 总结 前言 一、什么是重载函数&#xff1f; 在C中&#xff0c;函数重载&#x…

【Linux工具篇】---vim的基本使用

个人主页&#xff1a;平行线也会相交 欢迎 点赞&#x1f44d; 收藏✨ 留言✉ 加关注&#x1f493;本文由 平行线也会相交 原创 收录于专栏【Linux专栏】&#x1f388; 本专栏旨在分享学习Linux的一点学习心得&#xff0c;欢迎大家在评论区讨论&#x1f48c; 目录 &#x1f370…

C# 学习(一)概述

今天开始学习 C#&#xff0c;所有学习资料来源于&#xff1a; 菜鸟教程 一、C# 简介 C# 是 .NET 框架的一部分&#xff0c;随之创造出来的语言&#xff0c;所以了解 C# 前&#xff0c;需要知道 .NET 是个什么东西。 1.1 .NET 框架介绍 .NET 是微软提出的 Web 的一种软件开发…

智能文档图像处理技术应用与实践

写在前面智能文档处理面临的技术难题智能文档处理的研究领域● 文档图像分析与预处理● 手写板反光擦除● 版面分析与文档还原 写在最后 写在前面 VALSE 2023 无锡视觉与学习青年学者研讨会近期在无锡国际博览中心举办&#xff0c;由江南大学和无锡新吴区联合承办。本次会议旨…

卡尔曼滤波在目标跟踪中的应用(4)

在前一节内容中&#xff0c;我们学习了二维匀加速运动目标的卡尔曼滤波问题&#xff0c;同时利用MATLAB进行了仿真验证&#xff0c;今天我们继续往下扩展一个维度&#xff0c;学习三维空间下的卡尔曼滤波问题。 话不多说&#xff0c;开整&#xff01;&#xff01;&#xff01; …

【深度学习】6-1 卷积神经网络 - 卷积层

卷积神经网络(Convolutional Neural Network&#xff0c;CNN)。 CNN 被用于图像识别、语音识别等各种场合&#xff0c;在图像识别的比赛中&#xff0c;基于深度学习的方法几乎都以 CNN 为基础。 首先&#xff0c;来看一下 CNN 的网络结构&#xff0c;了解 CNN 的大致框架。CNN…

算法程序设计 之 装载问题(6/8)

一、实验目的&#xff1a; 理解并掌握回溯法与分支限界法的联系与区别&#xff0c;学会构造不同问题的解空间树&#xff0c;用上述两种算法解决装载问题。 实验内容问题描述&#xff1a;有n个集装箱要装上2艘载重量分别为C1和C2的轮船&#xff0c;其中集装箱i的重量为wi&#…

论文浅尝 | DEER:解释实体关系的描述性知识图谱

笔记整理&#xff1a;王润哲&#xff0c;东南大学硕士&#xff0c;研究方向为多元关系抽取 链接&#xff1a;https://aclanthology.org/2022.emnlp-main.448.pdf 动机 实体关系是知识图谱中不可或缺的一层重要信息&#xff0c;它们描述了实体之间的语义关系&#xff0c;这种连接…

【力扣刷题 | 第十二天】

目录 前言&#xff1a; 104. 二叉树的最大深度 - 力扣&#xff08;LeetCode&#xff09; 111. 二叉树的最小深度 - 力扣&#xff08;LeetCode&#xff09; 前序遍历&#xff1a; 后序遍历&#xff1a; 总结&#xff1a; 前言&#xff1a; 今天还是对树的基础题进行刷题&am…

大数据Doris(四十七):开启Steam Load记录

文章目录 开启Steam Load记录 一、停止 Doris 集群 二、在 node3-node5 BE 节点上配置 be.conf 三、重新启动 Doris 集群 开启Steam Load记录 后续执行Stream Load 导入任务后&#xff0c;我们会在Doris集群中会查询对应Stream Load任务的情况&#xff0c;默认BE是不记录S…

【Rust日报】2023-06-20 使用Quickwit、Jaeger和Grafana监控您的Rust应用程序

使用Quickwit、Jaeger和Grafana监控您的Rust应用程序 你可能已经看过了Lucas Palmieri的博客文章Are we observable yet? An introduction to Rust telemetry。如果你还没有看过&#xff0c;我们建议阅读一下&#xff0c;因为它提供了一个全面的介绍&#xff0c;介绍了如何处理…

【Python 基础篇】Python 函数:代码重用的利器

文章目录 导言一、创建函数二、函数参数1、位置参数2、关键字参数3、默认参数 三、函数返回值四、函数的高级用法1、递归函数2、匿名函数3、内置函数 总结 导言 函数是一种在Python中定义和封装可重用代码的重要机制。它们使我们能够将复杂的任务分解为更小的部分&#xff0c;…

【算法与数据结构】15、LeetCode三数之和

文章目录 一、题目二、双指针法三、完整代码 所有的LeetCode题解索引&#xff0c;可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、双指针法 思路分析&#xff1a;我们使用双指针法&#xff0c;但这道题因为要求数组三个元素的和&#xff0c;一共用到了三个指…