客户端验证和服务器端验证哪个更好?
在我们的情况下,我们使用
jQuery和MVC。
JSON数据在视图和控制器之间传递。
我所做的很多验证都是在用户输入数据时进行验证。
例如,我使用按键事件来防止文本框中出现字母,设置一个最大字符数,并在一个范围内设置一个数字。
我想更好的问题应该是,做服务器端验证比客户端验证有什么好处吗?
很棒的回答每个人。我们拥有的网站是密码保护的,用户基数小(<50)。如果他们没有运行JavaScript,我们将发送忍者。但如果我们为每个人设计一个网站,我同意对双方进行验证。
正如其他人所说,你应该两者兼顾。原因如下:
客户端
您希望首先验证客户端的输入,因为这样可以为普通用户提供更好的反馈。例如,如果他们输入了无效的电子邮件地址并移动到下一个字段,您可以立即显示错误消息。这样用户就可以在提交表单之前纠正每个字段。
如果您只在服务器上进行验证,则他们必须提交表单,获得错误消息,并尝试查找问题。
(这种痛苦可以通过让服务器重新呈现用户原始输入的表单来缓解,但客户端验证仍然更快。)
服务器端
您希望在服务器端进行验证,因为这样可以防止恶意用户,他们可以轻松绕过JavaScript并向服务器提交危险的输入。
相信你的UI是非常危险的。他们不仅会滥用你的UI,还可能根本不使用你的UI,甚至根本不使用浏览器。如果用户手动编辑URL,或者运行他们自己的Javascript,或者使用其他工具调整他们的HTTP请求呢?例如,如果它们从curl或脚本发送自定义HTTP请求呢?
(这不是理论上的;我曾经在一个旅游搜索引擎上工作过,它通过发送POST请求将用户的搜索重新提交给许多合作航空公司、公交公司等,就像用户已经填写了每个公司的搜索表单一样,然后收集和排序所有结果。这些公司的表单JS从未执行过,他们在返回的HTML中提供错误消息对我们来说至关重要。当然,一个API会很好,但这是我们必须做的。)
从安全的角度来看,不允许这样做不仅是幼稚的,而且是不标准的:应该允许客户端以任何他们希望的方式发送HTTP,并且您应该正确地响应。这包括验证。
服务器端验证对于兼容性也很重要——并非所有用户(即使他们使用浏览器)都会启用JavaScript。
附录- 2016年12月
There are some validations that can't even be properly done in server-side application code, and are utterly impossible in client-side code, because they depend on the current state of the database. For example, "nobody else has registered that username", or "the blog post you're commenting on still exists", or "no existing reservation overlaps the dates you requested", or "your account balance still has enough to cover that purchase." Only the database can reliably validate data which depends on related data. Developers regularly screw this up, but PostgreSQL provides some good solutions.
好吧,我还是可以回答的。
除了Rob和Nathan的回答,我还想补充一点,客户端验证很重要。当你在你的web表单上应用验证时,你必须遵循以下准则:
客户端
必须使用客户端验证,以过滤来自您网站的真实用户的真实请求。
应该使用客户端验证来减少在服务器端处理期间可能发生的错误。
应该使用客户端验证来最小化服务器端往返,从而节省带宽和每个用户的请求。
服务器端
You SHOULD NOT assume the validation successfully done at client side is 100% perfect. No matter even if it serves less than 50 users. You never know which of your user/emplyee turn into an "evil" and do some harmful activity knowing you dont have proper validations in place.
Even if its perfect in terms of validating email address, phone numbers or checking some valid inputs it might contain very harmful data. Which needs to be filtered at server-side no matter if its correct or incorrect.
If client-side validation is bypassed, your server-side validations comes to rescue you from any potential damage to your server-side processing. In recent times, we have already heard lot of stories of SQL Injections and other sort of techniques that might be applied in order to gain some evil benefits.
Both types of validations play important roles in their respective scope but the most strongest is the server-side. If you receive 10k users at a single point of time then you would definitely end up filtering the number of requests coming to your webserver. If you find there was a single mistake like invalid email address then they post back the form again and ask your user to correct it which will definitely eat your server resources and bandwidth. So better you apply javascript validation. If javascript is disabled then your server side validation will come to rescue and i bet only a few users might have accidentlly disable it since 99.99% of websites use javascript and its already enabled by default in all modern browsers.