"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > Django 숨겨진 파일 경로, 파일의 안전한 다운로드

Django 숨겨진 파일 경로, 파일의 안전한 다운로드

2025-04-13에 게시되었습니다
검색:151

How to Securely Serve Downloadable Files in Django by Obscuring File Paths?

임의의 문자열 또는 타임 스탬프를 실제 경로와 결합하여 각 파일에 대한 숨겨진 경로를 수동으로 생성하는 것입니다. 그런 다음 생성 된 경로는 다운로드 URL에서 사용하여 서버로 전달할 수 있습니다. 이를 통해 사용자는 URL을 추측하거나 조작하여 파일에 액세스 할 수 없도록합니다.

그러나이 방법에는 추가 개발 및 구성이 필요하므로 효율성이 떨어집니다. 보다 간소화 된 솔루션의 경우 다음을 고려하십시오.

x-sendfile 또는 x-accel-redirect를 사용하여 다음을 고려하십시오. Apache는 x-sendfile 헤더를 사용하고 Nginx는 x-Accel-Redirect를 사용합니다. By setting these headers in the HTTP response, the server will directly retrieve the file and send it to the user.

To implement this method:

    Set up mod_xsendfile or X-Accel-Redirect on your server.
  1. Update your Django view to generate the file path and set the appropriate header in the response. x-sendfile을 사용하는 예는 다음과 같습니다.
에서 django.utils.encoming import smart_str. 응답 = httpresponse (content_type = 'application/force-download') 응답
from django.utils.encoding import smart_str

response = HttpResponse(content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['X-Sendfile'] = smart_str(path_to_file)
return response
= smart_str (path_to_file) 반응 응답

이 접근법을 사용하여 Django는 보안을 유지하고 개발에 시간과 노력을 절약하면서 효과적으로 다운로드 가능한 파일을 제공 할 수 있습니다.

최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3