CURL和Postman无法通过LoginRequiredMixin访问Django rest api

CURL和Postman无法通过LoginRequiredMixin访问Django rest api

问题描述:

我遇到了使用cURL和Postman测试Django rest api的问题。当未经授权的用户试图访问,他将被重定向到登录页面的页面CURL和Postman无法通过LoginRequiredMixin访问Django rest api

class UserList(LoginRequiredMixin, generics.ListCreateAPIView): 
    model = User 
    queryset = User.objects.all() 
    serializer_class = UserSerializer 

:我使用LoginRequiredMixin来限制我的ClassView中访问。在URL中是一个?next参数,以便用户在授权后立即查看所需的页面。

/accounts/login/?next=/users/ 

问题是,卷曲和邮差可能甚至不使用身份验证提供的用户名和密码,并立即重定向到其返回结果的登录页面。

以下是一个示例或cURL命令。即使提供了用户名和密码,结果也是302 Found。当我为以下重定向添加-L参数时,它会返回来自登录页面的响应,并且不会重定向回原始页面。

curl -i -L -u superadmin:superadmin http://127.0.0.1:8000/users/ 
HTTP/1.0 302 Found 
Date: Fri, 13 Oct 2017 10:16:31 GMT 
Server: WSGIServer/0.2 CPython/3.5.2 
Vary: Cookie 
Content-Length: 0 
Content-Type: text/html; charset=utf-8 
Location: /accounts/login/?next=/users/ 
X-Frame-Options: SAMEORIGIN 

HTTP/1.0 200 OK 
Date: Fri, 13 Oct 2017 10:16:31 GMT 
Server: WSGIServer/0.2 CPython/3.5.2 
Vary: Cookie 
Content-Length: 1128 
Content-Type: text/html; charset=utf-8 
Cache-Control: max-age=0, no-cache, must-revalidate, no-store 
X-Frame-Options: SAMEORIGIN 
Expires: Fri, 13 Oct 2017 10:16:31 GMT 
Set-Cookie: csrftoken=cCfAfsSlHOZEQGvPD1RR33r1UXj6JtEscWKFjmVyHmvVasqMx2J0pqyeNbVpY3X9; expires=Fri, 12-Oct-2018 10:16:31 GMT; Max-Age=31449600; Path=/ 

<html> 
    <head> 
     <title>Login</title> 
    </head> 
    <body> 
     <h1>Login</h1> 

     <form method="post" action=""> 
      <table> 
       <tr> 
        <td><label for="id_username">Username:</label></td> 
        <td><input type="text" name="username" id="id_username" autofocus maxlength="254" required /></td> 
       </tr> 
       <tr> 
        <td><label for="id_password">Password:</label></td> 
        <td><input type="password" name="password" id="id_password" required /></td> 
       </tr> 
       <tr> 
        <td colspan="2"> 
         <input type="submit" value="Login" /> 
         <input type="hidden" name="next" value="/private/meals/" /> 
         <input type='hidden' name='csrfmiddlewaretoken' value='Pd3g7jmZ0WAACWihmRxNGvLF2wy5yzP9Pxylbdpc0u6RWIdegSpW2SSSVKaoN98Q' /> 
        </td> 
       </tr> 
      </table> 
     </form> 

     <p><a href="/accounts/signup/">Sign up</a></p> 
    </body> 
</html> 

我试着保存和加载cookie建议here但它也不起作用。有没有办法在cURL和Postman中传递LoginRequiredMixin?或者,Django Rest Framework中适当的访问限制方式可以与Rest API测试人员一起工作。

谢谢

你尝试使用-e选项?

curl manpage

-e, --referer <URL> 

    (HTTP) Sends the "Referrer Page" information to the HTTP server. This can also be set with the -H, --header flag of course. When used with -L, --location you can append ";auto" to the -e, --referer URL to make curl automatically set the previous URL when it follows a Location: header. The ";auto" string can be used alone, even if you don't set an initial -e, --referer. 

    If this option is used several times, the last one will be used. 

    See also -A, --user-agent and -H, --header. 
+0

不幸的是,它也没有帮助。我没有为其他浏览器使用不同的代码,也不检查用户来自哪里。我不知道如何使用--header选项或它应该如何帮助。我开始认为cURL和Postman甚至都没有使用提供的认证,并且由Django中的LoginRequiredMixin立即重定向。我不确定在Django中有什么其他好访问限制。 –

+0

您是否说过存储cookie不起作用?如: curl -X POST -d“username = superadmin&password = superadmin”-c登录http:// localhost:8000/accounts/login; curl -b登录http:// localhost:8000/users/ – efkin

+0

哇。通过一个小修改它实际上工作。通过POSTing数据登录页面登录后,我得到“403 Forbidden - CSRF验证失败,请求中止。”信息。这很奇怪,因为我已正确设置登录页面中的csrf标记。尽管如此,我尝试设置csrf作为参数来建立curl命令[这里](https://*.com/questions/10628275/how-to-use-curl-with-django-csrf-tokens-and-post-requests)它的工作。
你有什么想法如何与邮差做。你也可以发布它作为答案。我会这样做,但不想承担你的功劳。 –

您需要使用适当的身份验证机制。你需要检查你使用的是什么AUTHENTICATION_BACKEND。很有可能你会使用基于会话的认证,这意味着你需要在Postman中添加一个包含认证会话的Cookie。

要获取Cookie,请通过Chrome登录您的网站,打开Developer Tools -> Storage -> Cookies。找到您的Domain并将NameValue复制到您的域的Postman app -> Cookies -> Manage Cookies

这不是执行身份验证的适当REST API方式。邮差提供对varied authentication mechanisms的支持,如基本身份验证(您正在尝试使用用户名/密码),OAuth,摘要身份验证和其他几种技术。如果您打算为您的后端构建REST API,则最好使用Django API框架,如Django REST FrameworkDjango Tastypie