如何防止直接访问CSS和JS文件
可能的重复:
如何阻止直接访问我的 JavaScript 文件?< /a>
我有一个文件夹,里面还有大约五个文件夹,如下所示:
1.asset(folder)
1.1 css (subfolder)
1.1.1 style.css (file)
1.2 javascript (subfolder)
1.2.1 validation.js (file)
// goes on like this
现在我的问题是,当任何用户在浏览时点击 - my_base_url/asset/css/style.css 时,如何防止我的 css 出现?
我尝试了空白index.html,但它不起作用。我还尝试使用“拒绝所有”.htaccess 文件,但在这种情况下,当我加载我的网站时,它找不到 CSS。
Possible Duplicate:
How can I block direct access to my JavaScript files?
I have a folder and inside it there are around five more which looks like following:
1.asset(folder)
1.1 css (subfolder)
1.1.1 style.css (file)
1.2 javascript (subfolder)
1.2.1 validation.js (file)
// goes on like this
Now my question is how to prevent my css to appear when any user hits - my_base_url/asset/css/style.css on the browse?
I tried blank index.html but it doesn't work. I also tried using "deny from all" .htaccess file but in this case when I load my website it doesn't find the CSS..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从技术上讲,您所要求的是不可能的。
为了让浏览器能够使用 CSS,它必须能够访问它。 JavaScript 同上。
您能做的最好的事情就是混淆来源。您可以通过使用经过处理的语言(例如用于 CSS 的 Stylus 或 LessCSS,以及用于 JS 的 Coffeescript)来实现此目的,或者使交付更加复杂。您可以缩小脚本文件,甚至让它们动态生成并由另一个 JS 文件(小块)获取。
但是,您所做的任何事情都可以进行逆向工程,因为用户必须能够访问规则才能让浏览器使用它们。
What you're asking for is, technically, impossible.
In order for the browser to be able to use the CSS, it must have access to it. Ditto Javascript.
The best you can do is obfuscate the sources. You could do this by using a processed language - such as Stylus or LessCSS for CSS, and Coffeescript for JS - or by making the delivery more complicated. You could minify the script files, or even have them dynamically generated and fetched (in small pieces) by another JS file.
However, anything you do can be reverse-engineered, because the users must be able to access the rules in order for the browser to use them.
如果您不想显示 css 和 js 目录的目录列表,请将以下内容添加到 .htaccess 文件中:
from: http://www.thesitewizard.com/apache/prevent-directory-listing-htaccess.shtml
If you mean you don't want to show the directory listing for your css and js directories, add the following to your .htaccess file:
from: http://www.thesitewizard.com/apache/prevent-directory-listing-htaccess.shtml
首先,阻止直接链接到css文件并不意味着其他人看不到内容。大多数浏览器都具有检查加载文件的功能,其中包括 css。
您可以做的一件事是将所有内容放在 WEB-INF 文件夹下,并让控制器加载所有静态资源。但我想不出你想要这样做的原因。
First of all, preventing direct link to the css file doesn't mean that other people can't see the content. Most browsers have features to inspect the files loaded, which includes your css.
One thing you could do is put everything under WEB-INF folder and have controllers to load all your static resources. I can't think of a reason why you want to do this though.