当前位置: 首页 > 知识库问答 >
问题:

CURL和Postman无法使用LoginRequiredMixin访问Django rest api

袁俊弼
2023-03-14

我在使用cURL和Postman测试Django restapi时遇到问题。我正在使用LoginRequiredMixin限制对我的类视图的访问:

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

当未经授权的用户试图访问该页面时,他将被重定向到登录页面。在URL中是一个?下一个参数,以便用户在授权后立即查看所需页面。

/accounts/login/?next=/users/

问题是cURL和Postman可能甚至不使用提供的用户名和密码进行身份验证,并立即被重定向到登录页面,结果返回。

下面是一个示例或cURL命令。即使提供了用户名和密码,结果也是302。当我为下面的重定向添加-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建议在这里,但它也不工作。有什么方法可以在cURL和邮递员中传递Login必需的Mixin吗?或者在Django Rest框架中使用Rest API测试人员的正确访问限制方式是什么。

非常感谢。

共有2个答案

鲁烨
2023-03-14

您是否尝试使用-e选项?

从卷曲手册页

-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. 
莘欣怿
2023-03-14

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

要获取Cookie,请通过Chrome登录您的网站,打开Developer Tools-

这不是进行身份验证的适当RESTAPI方法。Postman提供了对各种身份验证机制的支持,如基本身份验证(您正试图使用用户名/密码)、OAuth、摘要身份验证和其他一些技术。如果您打算为后端构建RESTAPI,那么最好使用DjangoAPI框架,如DjangoRESTFramework或DjangoTastypie。

 类似资料:
  • 我在干什么? 步骤1-启用邻近API: 为了使用接近API,它必须首先在谷歌开发控制台中启用。使用本教程,我为我的项目启用了对邻近API的支持 步骤2-获取凭据: 根据本教程,我需要得到客户端ID和秘密。这就是我困惑的地方。凭据->添加凭据->OAuth2.0客户端ID->选择Chrome App单选按钮(因为我使用的是Postman)->输入Postman的Chrome Web store UR

  • 我正在尝试使用postman对firebase进行RESTAPI调用。当我的安全规则是允许所有用户,包括未经授权的用户时,我已经设法从firebase读取。 但当我使用这个规则时: 我从邮递员那里得到“错误”:“许可被拒绝”。我为谷歌的web oauth2.0客户端做了请求令牌,并拿回了authorization_code令牌。 我试图在URL和标题中使用令牌,用GET尝试过 请帮忙。提前谢谢

  • 我在Eclipse中创建了一个名为TestWeb的新动态web项目。我添加了一个索引。将html添加到WebContent文件夹,并创建了一个web servlet,使servlet在/Test中可用。 我可以访问索引。html文件位于http://localhost:8080/TestWeb但是,我无法访问位于的servlethttp://localhost:8080/TestWeb/Test.

  • 我想公开我的系统,以便通过ssh进行访问。 运行此

  • 我有一个aws lambda函数,它返回以下响应: 但是我在前端角度应用程序中得到以下误差。 CORS策略阻止从来源“HTTP://127.0.0.1:8080”访问位于“https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/price”的XMLHttpRequest:对飞行前请求的响应没有通过访问控制检查:它没有HTTP ok状态。

  • 我正在尝试使用Postman,chrome扩展api客户端Postman通过http访问Dynamodb。这实际上是我在《Dynamodb应用设计模式》一书中读到的一种方法。似乎我做错了什么,因为我无法成功地验证和访问web服务。 我在Postman客户端中使用的url和标题以及我最近的请求参数和错误消息如下: 我的请求是: 我得到以下错误消息: {uuu type:“com.amazon.cor