简体   繁体   English

如何防止用户从控制台执行javascript代码?

[英]How to prevent user from executing javascript code from console?

I am storing some sensitive data on javascript side. 我在javascript端存储了一些敏感数据。 How can I prevent user from tampering these data. 如何防止用户篡改这些数据。 I can not do this on server side with ajax or anything else, as smoothness of the animation concerned. 我不能在服务器端使用Ajax或其他方式执行此操作,因为动画的平滑性。

Make your JavaScript inaccessible to someone using function scope etc. This isn't perfect but it can certainly make things a lot harder. 使使用函数范围等功能的人无法访问您的JavaScript。这不是完美的方法,但肯定会使事情变得更加困难。 For example, if you define: 例如,如果您定义:

var f = function() { return "test"; }
f();

Then it's easy to call window.f() 然后很容易调用window.f()

If instead you define as a self invoking function then you've just made it much more difficult for someone to call: 相反,如果您将其定义为自调用函数,那么您将使某人致电变得更加困难:

(function() { 
return test;
})();

This principle can then be extended - any variables defined within the function, will only have scope within it making it very difficult (but probably not impossible) to get hold of them. 然后可以扩展该原理-函数中定义的任何变量都将仅具有范围,这使得很难(但可能并非不可能)掌握它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM