URL路径中的身份验证令牌

时间:2020-06-01 15:45:32

标签: django django-rest-framework

我正在使用Django REST Framework pd.DataFrame({"A":[1,2,3], "B": [{"Mon":"Closed", "Tue":"Open", "Wed":"Closed"}, {"Mon":"Open", "Tue":"Open", "Wed":"Closed"}, {"Mon":"Open", "Tue":"Open", "Wed":"Open"}] })

对于某些URL,我不得不在URL路径中包含令牌:

A B count 1 {..} 2 2 {..} 1 3 {..} 0

因此urlpatterns是:

TokenAuthentication

视图是:

https://example.com/api/<auth_token>/something

但是urlpatterns = [ path('api/<auth_token>/something', views.SomeView.as_view()), ] 在这里不起作用,因为令牌位于URL路径中,而不位于标头中。如果可能的话,我也想扩展class SomeView(APIView): authentication_classes = (TokenAuthentication) permission_classes = (IsAuthenticated, ) def get(self, request, auth_token): ... 来处理URL中的令牌。

1 个答案:

答案 0 :(得分:1)

首先,将身份验证令牌放在url中是不安全的,因此不建议这样做(URL最终会出现在服务器日志和内容中)。

如果这是生死攸关的问题,我要做的是从Django rest扩展Bad Request: /api/auth/register类,仅覆盖TokenAuthentication方法。如果您查看source code,它将从标头获得令牌,运行一些验证并调用authenticate。所以我会做类似的事情:

self.authenticate_credentials(token)