带有身份验证的Django跨域媒体
项目描述
带有身份验证的Django跨域媒体
情况:您从与主Web应用程序域不同的域(好主意)提供媒体文件。您想使用nginx的内部重定向(X-Accel-Redirect)来授权媒体文件的分发。
问题:您无法访问媒体域上的用户会话,无法验证或授权媒体访问。
解决方案:您使用带有过期令牌的媒体URL进行处理,该令牌临时授权访问,并在需要时通过重定向刷新。
HTTP视图
以下是其在HTTP中的工作方式
- -> GET media.example.org/path/file.pdf
- <- 302 www.example.com/path/file.pdf
- -> GET www.example.com/path/file.pdf
- 如果没有授权 <- 403
- 如果已授权 <- 302 media.example.org/path/file.pdf?token=XYZ
- -> GET media.example.org/path/file.pdf?token=XYZ
- <- 200 file.pdf
- 过期后 -> GET media.example.org/path/file.pdf?token=XYZ
- 请参阅步骤2
Django中的应用
# Development
MEDIA_URL = '/media/'
# Production
MEDIA_URL = 'https://media.example.org/media/
INTERNAL_MEDIA_PREFIX = '/protected/'
from crossdomainmedia import (
CrossDomainMediaAuth, CrossDomainMediaMixin
)
class CustomCrossDomainMediaAuth(CrossDomainMediaAuth):
'''
Create your own custom CrossDomainMediaAuth class
and implement at least these methods
'''
SITE_URL = 'https://www.example.com'
def is_media_public(self):
'''
Determine if the media described by self.context
needs authentication/authorization at all
'''
return self.context['object'].is_public
def get_auth_url(self):
'''
Give URL path to authenticating view
for the media described in context
'''
obj = self.context['object']
raise reverse('view-name', kwargs={'pk': obj.pk})
def get_media_file_path(self):
'''
Return the file path relative to MEDIA_ROOT
'''
obj = self.context['object']
return obj.file.name
class CustomDetailView(CrossDomainMediaMixin, DetailView):
'''
Add the CrossDomainMediaMixin
and set your custom media_auth_class
'''
media_auth_class = CustomCrossDomainMediaAuth
一些其他有用的方法
# Get your media URLs with token outside of view
mauth = CustomCrossDomainMediaAuth({'object': obj})
mauth.get_full_media_url(authorized=True)
# Send file via nginx internal redirect response
mauth.send_internal_file()
Nginx配置
这是一个Nginx配置可能的样子。
server {
# Web server with session on domain
listen 443 ssl http2;
server_name www.example.com;
# ...
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $host;
# etc...
proxy_pass wsgi_server;
}
}
server {
# Media server with no session on domain
listen 443 ssl http2;
server_name media.example.org;
# ...
location /media/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $host;
# etc...
proxy_pass wsgi_server;
}
location /protected {
internal;
alias /var/www/media-root;
}
}
项目详情
关闭
django-crossdomainmedia-0.0.4.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 71265b33804d631da3ce7e5e8c960c50b6dcca5859bce6f3b777269ee751d605 |
|
MD5 | 2865a65c1fdf40ef5d2ce61b00a4f71e |
|
BLAKE2b-256 | cbd6ad536efc64d6f0b9e844bebe0f7c2c4ef20427033610b9a2e2611e341441 |
关闭
django_crossdomainmedia-0.0.4-py2.py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e8c487511353d92a04fc050e887bf9dff1638432339c2eb6b0bef32b3e53593f |
|
MD5 | 6849045a5dee6f478f7fa0fad22624ca |
|
BLAKE2b-256 | c4ba23f98509211657c1f7924458830d5bc4f28723f846df58c937ee6c5b2976 |