事情是这样的。移动端的同事在调用Django接口时,明明已经有登录态,却返回未登录。于是app以为是token过期,刷新token再请求,再提示未登录,如此反复,造成了死循环。
查看日志,发现是URL后面少了斜杠,由于Django中设置了APPEND_SLASH=True, 当URL后面缺少斜杠时,会自动拼上斜杠,并重定向。
那为什么重定向之后,登录态就丢失了呢?更奇怪的是看日志发现原本是一个DELETE方法的请求,重定向后变成了GET请求。
原因是这样的。
我们的凭证是放在HTTP的header的Authorization里面。而重定向的时候这个数据丢失了。
看了一下RFC 2616,里面写到
The new permanent URI SHOULD be given by the Location field in the
response. Unless the request method was HEAD, the entity of the
response SHOULD contain a short hypertext note with a hyperlink to
the new URI(s).If the 301 status code is received in response to a request other
than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might
change the conditions under which the request was issued.Note: When automatically redirecting a POST request af