django 处理基本的 http 身份验证
django handling basic http auth
目前我有以下代码来处理传入的 GET 请求:
#view.py
def handle_request(request):
if request.method == 'GET':
<do something>
return response
此代码可以处理以下形式的简单 GET 请求:
curl http://some_url/
但现在我想添加基本的 http 身份验证:
curl --user username:password http://some_url/
我想将我的 views.py 代码修改为:
def handle_request(request):
if request.method == 'GET':
if username == some_hard_coded_approved_username and password == corresponding_password:
<do something>
return response
else:
response = HttpResponse("")
response.status_code = 401
return response
如何实现这一行来解析来自 http 请求的用户名和密码:
if username == some_hard_coded_approved_username and password == corresponding_password:
您应该为用户分配一定的权限。
检查用户是否通过身份验证以及他是否具有权限。如果上述条件成立,那么您应该执行代码块。
像这样:
def handle_request(request):
if request.method == 'GET':
if request.user.is_authenticated and user.has_perm('custom_permission'):
<do something>
return response
else:
response = HttpResponse("")
response.status_code = 401
return response
你应该避免在代码中直接使用用户名和密码,因为如果你把它放在任何 vcs 中,任何人都可以看到你的用户密码并侵入你的系统。
对于 django 权限,请转到 here
已解决:
对于以下命令:
curl -H "Authorization: username_in_curl_cmd password_in_curl_cmd" http_url
以下代码处理基本的 http 身份验证:
#views.py
def handle_request(request):
if 'HTTP_AUTHORIZATION' in request.META:
[user, password] = request.META['HTTP_AUTHORIZATION'].split(" ")
# user = username_in_curl_cmd
# password = password_in_curl_cmd
if user == some_enivorment_variable and password == some_enivorment_variable
and request.method == 'GET':
<do something>
return response
return 401 response
@Exprator 的评论为我指明了正确的方向。挑战在于弄清楚 'HTTP_' 被添加到 header 并且 header 被转换为大写。
目前我有以下代码来处理传入的 GET 请求:
#view.py
def handle_request(request):
if request.method == 'GET':
<do something>
return response
此代码可以处理以下形式的简单 GET 请求:
curl http://some_url/
但现在我想添加基本的 http 身份验证:
curl --user username:password http://some_url/
我想将我的 views.py 代码修改为:
def handle_request(request):
if request.method == 'GET':
if username == some_hard_coded_approved_username and password == corresponding_password:
<do something>
return response
else:
response = HttpResponse("")
response.status_code = 401
return response
如何实现这一行来解析来自 http 请求的用户名和密码:
if username == some_hard_coded_approved_username and password == corresponding_password:
您应该为用户分配一定的权限。 检查用户是否通过身份验证以及他是否具有权限。如果上述条件成立,那么您应该执行代码块。
像这样:
def handle_request(request):
if request.method == 'GET':
if request.user.is_authenticated and user.has_perm('custom_permission'):
<do something>
return response
else:
response = HttpResponse("")
response.status_code = 401
return response
你应该避免在代码中直接使用用户名和密码,因为如果你把它放在任何 vcs 中,任何人都可以看到你的用户密码并侵入你的系统。
对于 django 权限,请转到 here
已解决:
对于以下命令:
curl -H "Authorization: username_in_curl_cmd password_in_curl_cmd" http_url
以下代码处理基本的 http 身份验证:
#views.py
def handle_request(request):
if 'HTTP_AUTHORIZATION' in request.META:
[user, password] = request.META['HTTP_AUTHORIZATION'].split(" ")
# user = username_in_curl_cmd
# password = password_in_curl_cmd
if user == some_enivorment_variable and password == some_enivorment_variable
and request.method == 'GET':
<do something>
return response
return 401 response
@Exprator 的评论为我指明了正确的方向。挑战在于弄清楚 'HTTP_' 被添加到 header 并且 header 被转换为大写。