简体   繁体   English

Laravel:防止直接访问公用文件夹中的文件

[英]Laravel: Prevent direct access of files from public folder

I am stuck on this part of my laravel application, Where I am asked to protect the files from directly accessed via url browser hit. 我被困在laravel应用程序的这一部分中,要求我保护该文件以免通过URL浏览器直接访问。 I have a public folder in which in a doc folder is present where all the documents are going to be uploaded. 我有一个公用文件夹,其中有一个doc文件夹,所有文档都将被上传到该文件夹​​中。 I just need a solution to prevent this where i can access docs directly from my application but any third party visitor can not view my docs (images,pdfs etc..). 我只需要一个解决方案来防止这种情况,我可以直接从我的应用程序访问文档,但是任何第三方访问者都不能查看我的文档(图像,pdf等)。

I have tried many solutions but its not at all working. 我已经尝试了许多解决方案,但根本无法正常工作。 I just want to things :- 1. Protect my docs through direct access. 我只想了解以下内容:1.通过直接访问保护我的文档。 2. Way of implementing it in laravel (via .htaccess) 2.在laravel中实现它的方式(通过.htaccess)

I know this can be possible through htaccess, but how? 我知道这可以通过htaccess实现,但是怎么办? Kindly help Please :) 请帮助:)

There are three approaches I can think of just now; 我现在可以想到三种方法:

  1. You intercept all image and video requests with Laravel, then using the router, serve up the content that the user was after, provided they are authorised. 您可以使用Laravel拦截所有图像和视频请求,然后使用路由器提供经过授权的用户所关注的内容。 THIS WILL BE SLOW!. 这会很慢!
  2. You rely on obscurity and put all that clients images, videos etc in a folder that has a long-unguessable random url. 您依靠模糊性,将所有客户端图像,视频等放置在一个具有冗长的随机URL的文件夹中。 You can then link to the content in your code using the 'static' folder name. 然后,您可以使用“静态”文件夹名称链接到代码中的内容。 The customer's content will always be in that folder and accessible if they log in or not. 客户的内容将始终位于该文件夹中,并且无论是否登录,都可以访问。 The advantage of this compared to 1 is that your framework does not have to boot for every image or video. 与1相比,它的优点是您的框架不必为每个图像或视频都启动。
  3. Have all the content hidden away - possibly in the storage folder. 隐藏所有内容-可能在存储文件夹中。 When the user logs in, create a temporary symbolic link between their public folder and their folder in storage. 用户登录时,在其公用文件夹和存储中的文件夹之间创建一个临时的符号链接。 Keep a note of the link in the session. 记下会话中的链接。 Use the link in all gallery etc rather than the static code used in (2) above. 使用所有图库等中的链接,而不要使用上面(2)中使用的静态代码。 Once they log out the code will no longer be valid, and you can delete the symbolic link on logout or have a job to tidy it up periodically. 他们注销后,该代码将不再有效,您可以在注销时删除符号链接,也可以定期进行整理。

在您的上传文件夹.htaccess文件中添加以下内容:

Deny from  all

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

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