热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在Web服务器中存储文件的安全方法?-Securewaytostorefilesinwebserver?

Iwantmyfilestobesecureinmywebserver.Onlyauthenticateduserstoaccessthosefilesshould

I want my files to be secure in my web server. Only authenticated users to access those files should be able to access those files. I thought of storing files in database as "Long BLOB" but it supports only upto 2MB of data. The file size may exceed beyond 50MB. is there any other better way to secure the files? please help me.thanks in advance.

我希望我的文件在我的Web服务器中是安全的。只有经过身份验证的用户才能访问这些文件。我想将数据库中的文件存储为“Long BLOB”,但它只支持最多2MB的数据。文件大小可能超过50MB。有没有其他更好的方法来保护文件?请帮助我。谢谢你。

7 个解决方案

#1


23  

Don't store them in a database. Put them in your web directory and secure them using .htaccess.

不要将它们存储在数据库中。将它们放在您的web目录中并使用.htaccess保护它们。

If you want to authenticate via other means, then store the files in a directory that isn't web-accessible but is readable by the user php runs as.

如果您想通过其他方式进行身份验证,请将文件存储在一个不可通过Web访问但可由用户运行的目录中运行。

#2


5  

A few options come to mind.

我想到了一些选择。

If you are using Apache you can use htaccess to password protect directories. (first googled link : http://www.Javascriptkit.com/howto/htaccess3.shtml)

如果您使用的是Apache,则可以使用htaccess来保护目录。 (第一个用Google搜索链接:http://www.Javascriptkit.com/howto/htaccess3.shtml)

or Store the files above the web server. Create a script in php that will allow authorised users to access them.

或将文件存储在Web服务器上方。在php中创建一个脚本,允许授权用户访问它们。

If you want to do it Via FTP, and you are running cpanel you may be able to create new ftp accounts. check yourdomain.com/cpanel to determine if you have it installed.

如果您想通过FTP进行操作,并且您正在运行cpanel,则可以创建新的ftp帐户。检查yourdomain.com/cpanel以确定您是否安装了它。

#3


4  

Storing files in DB is very bad practice. Very good practice to store only information about file. Name, extension. Files save on server like $id.$ext. It will be a good architecture. And when user download file, he take file with name in DB.
Sorry for my english.

在DB中存储文件是非常糟糕的做法。非常好的做法,只存储有关文件的信息。姓名,延期。文件保存在服务器上,如$ id。$ ext。这将是一个很好的架构。当用户下载文件时,他在DB中获取名称文件。对不起我的英语不好。

#4


1  

Downloadable files can be stored in htaccess protected folder/s. A script like the one below can be used to generate dynamic links for downloadable files.

可下载的文件可以存储在受htaccess保护的文件夹中。可以使用下面的脚本为可下载文件生成动态链接。

for ex. Secure download links. http://codecanyon.net/item/secure-download-links/309295

对于前安全的下载链接。 http://codecanyon.net/item/secure-download-links/309295

#5


1  

Discussion

If you opt to keep high value downloadable content files directly on the filesystem, the best thing to do is to keep them outside of the webroot. Then, your application will have to solve the problem of creating URLs (url encoding when necessary) for content (PDF's, Word Docs, Songs, etc..).

如果您选择将高价值可下载内容文件直接保存在文件系统上,最好的办法是将它们保留在webroot之外。然后,您的应用程序将必须解决为内容(PDF,Word Docs,歌曲等)创建URL(必要时的URL编码)的问题。

Generally, this can be achieved by using a query to retrieve the file path, then using the file path to send content to the user (with header() etc ..) when he or she clicks on an anchor (all of this without the user ever seeing the true, server side file path).

通常,这可以通过使用查询来检索文件路径,然后使用文件路径向用户发送内容(带有header()等等)来实现,当他或她点击一个锚点时(所有这些都没有用户看到真正的服务器端文件路径)。

If you do not want user A sharing URLs for high value downloadable content to user B, then your application must somehow make the links exclusively tied to user A. What can be done? Where should I start?

如果您不希望用户A为用户B共享高价值可下载内容的URL,那么您的应用程序必须以某种方式使链接专门与用户A相关联。可以做什么?我应该从哪里开始?

Obviously, you want to make sure user A is logged in during a session before he or she can download a file. What is not so obvious is how to prevent a logged in user B from using a URL sent from user A (to user B) to download A's digital content.

显然,您希望确保用户A在会话期间登录,然后才能下载文件。不太明显的是如何防止登录用户B使用从用户A(到用户B)发送的URL来下载A的数字内容。

Using $_SESSION to store the logged in user's ID (numerical, or string) and making that part of the eventual query (assuming content is tied to user purchases or something) will prevent a logged in user B from downloading things they have not purchased, but you will still incur the resource hit for processing the SQL empty set for items they have not purchased. This sounds like a good step two.

使用$ _SESSION存储登录用户的ID(数字或字符串)并进行最终查询的那部分(假设内容与用户购买或其他内容相关联)将阻止登录用户B下载他们尚未购买的内容,但是,对于他们尚未购买的商品处理SQL空集,您仍会遇到资源问题。这听起来是一个很好的第二步。

What about step one? Is there something that can prevent the need to do a query to begin with?

第一步怎么样?有什么东西可以阻止开始查询的需要吗?

Well, let us see. In HTML forms, one might use a XSRF token in a hidden field to verify that a submitted form actually originated from the web server that receives the POST/GET request. One token is used for the entire form.

好吧,让我们看看。在HTML表单中,可以在隐藏字段中使用XSRF令牌来验证提交的表单是否实际来自接收POST / GET请求的Web服务器。一个标记用于整个表单。

Given a page of user specific things to download (anchors), one could embed a single token (the same token, but different per page request) into each anchor's href attribute in the form of a query string parameter and store a copy of this token in $_SESSION.

给定一个用户特定要下载的页面(锚点),可以以查询字符串参数的形式将单个令牌(相同的令牌,但每页请求不同)嵌入到每个锚点的href属性中,并存储此令牌的副本在$ _SESSION中。

Now, when a logged in user B attempts to use a logged in user A's shared URL, the whole thing fails because user A and user B have different sessions (or, no session at all), and thus different tokens. In other words, "My link is the same as yours, but different." Anchors would be tied to the session, not just to the page, user, or content.

现在,当登录用户B尝试使用登录用户A的共享URL时,整个操作失败,因为用户A和用户B具有不同的会话(或者根本没有会话),因此具有不同的令牌。换句话说,“我的链接与你的相同,但不同。”锚点将与会话绑定,而不仅仅与页面,用户或内容绑定。

With that system in place, PHP can determine if a request for content is valid without getting the database involved (by comparing the submitted token to the one in $_SESSION). What is more, a time limit can be established in $_SESSION to limit the duration/lifetime of a valid XSRF token. Just use the time() function and basic math. Sixty minutes might be an ideal token lifetime for an anchor in this situation. Have the user login again if the token for a clicked anchor has expired.

有了这个系统,PHP可以确定内容请求是否有效而不涉及数据库(通过将提交的令牌与$ _SESSION中的令牌进行比较)。更重要的是,可以在$ _SESSION中建立时间限制,以限制有效XSRF令牌的持续时间/生命周期。只需使用time()函数和基本数学。在这种情况下,60分钟可能是锚的理想标记生命周期。如果单击锚点的令牌已过期,请让用户再次登录。

Summary

If you use files on a filesystem and store the paths in the database, make sure you do the following (at minimum), too.

如果您在文件系统上使用文件并将路径存储在数据库中,请确保您也执行以下操作(至少)。

  1. Apply proper file permissions to your content directory (outside of webroot).
  2. 将适当的文件权限应用于您的内容目录(在webroot之外)。
  3. Use random names for uploaded files.
  4. 对上传的文件使用随机名称。
  5. Check for duplicate file names before saving a file from an upload.
  6. 在保存上载文件之前,请检查重复的文件名。
  7. Only logged in users should be able to download high value content.
  8. 只有登录用户才能下载高价值内容。
  9. Have an effective $_SESSION system that deters session fixation.
  10. 有一个有效的$ _SESSION系统,可以阻止会话固定。
  11. Make URLs for high value downloadable content are unique per page by using hashed XSRF tokens.
  12. 通过使用散列XSRF令牌,使每页的高价值可下载内容的URL是唯一的。
  13. XSRF tokens cover more scenarios when they have a terminal life time.
  14. 当XSRF令牌具有终端生命周期时,它们涵盖了更多场景。
  15. Make SQL queries for user content based on the logged in user's ID, not the product exclusively.
  16. 根据登录用户的ID而不是产品专用,对用户内容进行SQL查询。
  17. Filter and validate all user input.
  18. 过滤并验证所有用户输入。
  19. Use prepared statements with SQL queries.
  20. 使用带有SQL查询的预准备语句。

#6


0  

The best way is to store the file reference in Database. The file itself will be stored in the server filesystem. The complexity of this is making sure there is reference integrity between the database file reference and the existing file in the server filesystem. Some database such as sql server 2008 have feature that maintain the integrity of the file references to the actual file itself.

最好的方法是将文件引用存储在数据库中。文件本身将存储在服务器文件系统中。这种复杂性确保了数据库文件引用与服务器文件系统中现有文件之间存在引用完整性。某些数据库(如sql server 2008)具有维护对实际文件本身的文件引用的完整性的功能。

Other than that securing the file itself in the server depends on the OS where permissions can be configured to the specific folder where the file reside.

除了在服务器中保护文件本身之外,还取决于可以将权限配置到文件所在的特定文件夹的操作系统。

#7


0  

If the files are purely static you could use read-only or WORM media to store the data files or indeed run the complete web server from a "LiveCD". It's certainly not suited to everyone's needs but for limited cases where the integrity of the data is paramount it works.

如果文件是纯静态的,您可以使用只读或WORM介质来存储数据文件,或者实际上从“LiveCD”运行完整的Web服务器。它当然不适合每个人的需求,但对于有限的情况,数据的完整性至关重要。


推荐阅读
  • MySQL 数据库迁移指南:从本地到远程及磁盘间迁移
    本文详细介绍了如何在不同场景下进行 MySQL 数据库的迁移,包括从一个硬盘迁移到另一个硬盘、从一台计算机迁移到另一台计算机,以及解决迁移过程中可能遇到的问题。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • [论文笔记] Crowdsourcing Translation: Professional Quality from Non-Professionals (ACL, 2011)
    Time:4hoursTimespan:Apr15–May3,2012OmarZaidan,ChrisCallison-Burch:CrowdsourcingTra ... [详细]
  • 深入解析JVM垃圾收集器
    本文基于《深入理解Java虚拟机:JVM高级特性与最佳实践》第二版,详细探讨了JVM中不同类型的垃圾收集器及其工作原理。通过介绍各种垃圾收集器的特性和应用场景,帮助读者更好地理解和优化JVM内存管理。 ... [详细]
  • 本文详细分析了JSP(JavaServer Pages)技术的主要优点和缺点,帮助开发者更好地理解其适用场景及潜在挑战。JSP作为一种服务器端技术,广泛应用于Web开发中。 ... [详细]
  • 在当前众多持久层框架中,MyBatis(前身为iBatis)凭借其轻量级、易用性和对SQL的直接支持,成为许多开发者的首选。本文将详细探讨MyBatis的核心概念、设计理念及其优势。 ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • 作为一名新手,您可能会在初次尝试使用Eclipse进行Struts开发时遇到一些挑战。本文将为您提供详细的指导和解决方案,帮助您克服常见的配置和操作难题。 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 将Web服务部署到Tomcat
    本文介绍了如何在JDeveloper 12c中创建一个Java项目,并将其打包为Web服务,然后部署到Tomcat服务器。内容涵盖从项目创建、编写Web服务代码、配置相关XML文件到最终的本地部署和验证。 ... [详细]
  • MongoDB集群配置:副本集与分片详解
    本文详细介绍了如何在MongoDB中配置副本集(Replica Sets)和分片(Sharding),并提供了具体的步骤和命令,帮助读者理解并实现高可用性和水平扩展的MongoDB集群。 ... [详细]
author-avatar
曾经的我们太矫情_377
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有