我目前正在使用带有 django-rest-framework 包的 Python/Django 做一个 Restful API。我试图从 PHP 客户端进行调用,其中必须设置 cookie 以进行身份验证。
在我的 django 端,这就是所做的:
def SetToken(request):
"""
Set token string to cookie
"""
if 'token' in request.GET:
token_str = request.GET['token']
response = HttpResponse()
response.set_cookie("ttsessid", token_str)
return response
因此,cookie“ttsessid”被创建。但是,我意识到长令牌值将导致创建引用的 cookie 值。这些是一些示例场景:
示例 1:短字符串,无引号:
token = 2xYlyVhBKRgtAY2dzFMWDwoedV569keWKJfpNZCqLdMaQe5x38Bi9j5Pept3E%2BBdSSRyfq9zgR8KYnAqG4p%2Pept3E%2BBdSSRyfq9zgR8KYnAqG4p%2
"ttsessid" value = 2xYlyVhBKRgtAY2dzFMWDwoedV569keWKJfpNZCqLdMaQe5x38Bi9j5Pept3E%2BBdSSRyfq9zgR8KYnAqG4p%2Pept3E%2BBdSSRyfq9zgR8KYnAqG4p%2
示例 2:更长的字符串,双引号导致:
token = 2xYlyVhBKRgtAY2dzFMWDwoedV569keWKJfpNZCqLdMaQe5x38Bi9j5Pept3E%2BBdSSRyfq9zgR8KYnAqG4p%2Pept3E%2BBdSSRyfq9zgR8KYnAqG4p%2BoHOs2AvfQ2pPP7eiLaxBCkRaCC4SUZ8zvC%2F9DwbcTXSMnj5H0qJVt1XjnIPVLJ6GcWd6t9
"ttsessid" value = "2xYlyVhBKRgtAY2dzFMWDwoedV569keWKJfpNZCqLdMaQe5x38Bi9j5Pept3E%2BBdSSRyfq9zgR8KYnAqG4p%2Pept3E%2BBdSSRyfq9zgR8KYnAqG4p%2BoHOs2AvfQ2pPP7eiLaxBCkRaCC4SUZ8zvC%2F9DwbcTXSMnj5H0qJVt1XjnIPVLJ6GcWd6t9"(Notice the double quotes)
我检查了字符串。它不包含任何禁止的字符,例如“=”或“&”,所以我不确定它有什么问题。
我希望有人可以帮助我解决这个问题。谢谢!