简体   繁体   English

检查传入请求的授权HTTP Header,Django

[英]Check the Authorization HTTP Header of incoming request, Django

I should check the Authorization HTTP Header of every incoming request.我应该检查每个传入请求的授权 HTTP Header。 In case of missing or invalid Authorization header.如果授权 header 丢失或无效。 How to make simple authorization header validation?如何进行简单的授权 header 验证?

I don't think it's the best way and think it's ugly.我不认为这是最好的方法,而且认为它很丑陋。 Nonetheless, here's my solution:尽管如此,这是我的解决方案:

# i did this in views.py
class SomeView(View):
   def get(self, request):
      for key, value in request.header.items():
         # loop through the header...

In case your token is set to something like this:如果您的令牌设置为如下所示:

Token <HTTP_AUTHORIZATION_TOKEN>

you could do something like that:你可以这样做:

s, token = request.META["HTTP_AUTHORIZATION"].split(" ")

This gets the authorization header and seperates it into two strings.这将获得授权 header 并将其分成两个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM