Django安全用户到本地系统目录映射

2024-09-29 23:33:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,我必须安全地让登录的用户从指定的路径访问本地目录内容。/djangapp/media/user1也就是说,当user1登录时,他们应该只能从/djangapp/media/user1访问内容

我目前的看法是:

def get_absolute_pathname(pathname='', safe=True):
if not pathname:
    return os.path.join(MEDIA_ROOT, 'index')
if safe and '..' in pathname.split(os.path.sep):
    return get_absolute_pathname(pathname='')
return os.path.join(MEDIA_ROOT, pathname)       


@login_required
def retrieve_path(request, document_root,  pathname=''):
pathname = None
if request.user.is_authenticated():
  pathname = request.user.get_username()
  abs_pathname = get_absolute_pathname(pathname)
  url = document_root
  response = HttpResponseRedirect(url)
  return response

当前URL为:

^{pr2}$

我可以直接从url访问http://127.0.0.1:8000/DjangoApp/static/。但我想限制访问。在

我做错了什么?如何使访问通过身份验证并且仅限于固定路径?在

谢谢你


Tags: path路径url内容getreturnifos
1条回答
网友
1楼 · 发布于 2024-09-29 23:33:04

安全的媒体文件不被匿名用户服务更好的网址保护。。。在

使用@login_requireddef protected_serve(request, path, document_root=None):可以保护它。。在

更多信息How to to make a file private by securing the url that only authenticated users can see

相关问题 更多 >

    热门问题