简体   繁体   English

拒绝直接访问IIS Web服务器中的JavaScript文件

[英]Deny Direct Access to JavaScript Files in IIS Web Server

I have a web application which is in php and java script. 我有一个使用php和java脚本的Web应用程序。 if someone tries to enter the path of the java script file in the browser, the complete java script is being displayed in the browser. 如果有人尝试在浏览器中输入Java脚本文件的路径,则将在浏览器中显示完整的Java脚本。 For example: http://myserver.com/MyApp/app/view/baseView.js , this returns the source code of that particular java script file. 例如: http : //myserver.com/MyApp/app/view/baseView.js ,这将返回该特定Java脚本文件的源代码。

Can I restrict this ? 我可以限制吗? I am using Windows IIS 7.5 Web Server. 我正在使用Windows IIS 7.5 Web服务器。 I have tried doing this in the main web.config file: 我尝试在主web.config文件中执行此操作:

<security>
        <authorization>
            <remove users="*" roles="" verbs="" />
            <add accessType="Allow" users="" roles="Administrators" />

        </authorization>
    </security>

But this code blocks the complete application, even when I try to access the default.php file through the url, the following error is being returned by the Web Server : "401 - Unauthorized: Access is denied due to invalid credentials". 但是,即使我尝试通过url访问default.php文件,此代码也会阻止整个应用程序,Web服务器将返回以下错误:“ 401-未经授权:由于无效的凭据而拒绝访问”。

Please help ! 请帮忙 !

You cannot block direct access to the file. 您不能阻止直接访问该文件。 If you restrict access to the file, as you mentioned the php file will 'break' for users who don't have access to that js file. 如前所述,如果您限制对该文件的访问,则对于无法访问该js文件的用户,php文件将“中断”。

You could however redirect users who visit the URL to the file directly: Javascript example: 但是,您可以将访问URL的用户直接重定向到文件:Javascript示例:

<script>
if(window.location.href.endsWith('baseView.js'))
{
    window.location = 'www.google.com'
}
</script>

You may also be able to use the .htaccess file to do this, which would be much better. 您可能还可以使用.htaccess文件来执行此操作,这会更好。 (although I cannot test this myself right now, I'm not sure if it would work and not be invoked through the php file) (尽管我现在无法自己进行测试,但是我不确定它是否可以工作并且不能通过php文件调用)

Note: This will in no way stop users from reading the Javascript file, so I am not sure why you want to do this. 注意:这绝不会阻止用户阅读Javascript文件,因此我不确定为什么要这样做。

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

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