- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Django 和 django-rest-framework 构建一个 RESTful API .
作为身份验证机制,我们选择了“ token 身份验证”,并且我已经按照 Django-REST-Framework 的文档实现了它,问题是,应用程序是否应该定期更新/更改 token ,如果是,如何更新?是移动应用程序需要更新 token 还是应该由网络应用程序自主完成?
什么是最佳实践?
这里有人使用过 Django REST Framework 并且可以提出技术解决方案吗?
(最后一个问题优先级较低)
最佳答案
让移动客户端定期更新其身份验证 token 是一种很好的做法。这当然要由服务器执行。
默认的 TokenAuthentication 类不支持这个,但是你可以扩展它来实现这个功能。
例如:
from rest_framework.authentication import TokenAuthentication, get_authorization_header
from rest_framework.exceptions import AuthenticationFailed
class ExpiringTokenAuthentication(TokenAuthentication):
def authenticate_credentials(self, key):
try:
token = self.model.objects.get(key=key)
except self.model.DoesNotExist:
raise exceptions.AuthenticationFailed('Invalid token')
if not token.user.is_active:
raise exceptions.AuthenticationFailed('User inactive or deleted')
# This is required for the time comparison
utc_now = datetime.utcnow()
utc_now = utc_now.replace(tzinfo=pytz.utc)
if token.created < utc_now - timedelta(hours=24):
raise exceptions.AuthenticationFailed('Token has expired')
return token.user, token
还需要覆盖默认的 rest framework 登录 View ,以便在登录完成时刷新 token :
class ObtainExpiringAuthToken(ObtainAuthToken):
def post(self, request):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
token, created = Token.objects.get_or_create(user=serializer.validated_data['user'])
if not created:
# update the created time of the token to keep it valid
token.created = datetime.datetime.utcnow()
token.save()
return Response({'token': token.key})
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
obtain_expiring_auth_token = ObtainExpiringAuthToken.as_view()
并且不要忘记修改 url:
urlpatterns += patterns(
'',
url(r'^users/login/?$', '<path_to_file>.obtain_expiring_auth_token'),
)
关于django - RESTful API 的 token 身份验证 : should the token be periodically changed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14567586/
我写了一个 mdx 脚本来显示 Internet Sales Amount 的周期增长,一切正常。我们正在使用一个界面,您可以在其中放置一个切片器,以便用户可以选择他感兴趣的 date.Calenda
我有一列数据,例如 '1971q1'、'1972q2' 等(年份后跟季度)当我这样做时: print(type(df.Quarterly)) 答案是系列 我需要的是将此列“转换”/转换为真正的 pd.
下面是我要处理的句子。它由前面以两个哈希值 (##2312435) 开头的 ID 以及其余文本组成。我需要一个正则表达式来查找其中包含单词 likely 的句子,并检索整个句子和 ID。 句子: ##
这两条线有什么区别? call apoc.periodic.iterate("MATCH (n:Nodes) return n", "DETACH DELETE n", {batchSize:1000
这个问题在这里已经有了答案: When does Thread.CurrentThread.Join() make sense? (6 个答案) 关闭 9 年前。 有人可以向我解释 Thread.S
我正在尝试用 java.time 替换 org.joda.time.Period。 我们在数据库中存储了以下值。 P1M, P1Y, P1D, PT1H, PT1M 只是解析这个值: Period m
我正在尝试解析 linux dig 的输出使用正则表达式在一次拍摄中执行多项操作。 假设我挖掘主机 mail.yahoo.com : /usr/bin/dig +nocomments +noquest
我正在尝试创建一个匹配以下内容的正则表达式: 一个或多个数字 允许在第一个数字后有 0 到 1 个句点 如果有句号 句点后需要 1 - 2 位数字 这是我目前使用的正则表达式,它并不适用于所有情况:
这是我的代码。我想绘制美国东部夏令时9:30-10点的最高点和最低点。我破解了这个代码,但我搞不清时间了。只有当我将其设置为0800-0900时才有效,这没有任何意义。如果我把它设置为0930-100
我正在制作一个仅用于学习目的的应用程序。我试图计算两个日期之间的差异 LocalDate today = LocalDate.now(); LocalDate localHoudbaarheid =
我正在使用 SEO 友好的 URL,我可以通过 ASP.NET 处理其中的大部分,通过将 aspnet_isapi.dll 映射到所有 URL。 (我在 IIS 中设置了一个处理程序映射,它对所有路径
我准备了以下数据 Timestamp Weighted Value SumVal Group 1 1600 800 1 2
为什么结果为periodBetween.Days和 substracted.Days不同的? 我可以看到一个 periodBetween.Months是 0 和 substracted.Months是
我有生日和第二个 LocalDate.now()我使用 Periof 打印这个 Period test = Period.between(dateofbirth,LocalDa
我使用 cocoa 在 Mac 上开发了一个应用程序。我需要将 cmd + period(.) 键盘事件作为我设计的命令来处理。但现在 cmd + period(.) 键盘事件没有像我预期的那样得到很
我有4000个观测值的数据,所以这是head(both): kön gdk age fbkurs pers stterm 1 man
我有一个包含特定类元素 vector 的类。主要思想是根据序列的一个周期(elems_)和周期数(nperiod_)生成元素的周期性序列,因此我不需要存储所有元素,而只需存储一个周期。 class P
我正在尝试添加句点“。”以及变量的后缀“-on”(下面第 4 行): function globalProducts(cat, self){ $("div.info").hide();
例如: requests.get() 是一个带有句点的函数。我如何制作自己的函数,例如: def foo(): return 'bar' 并像这样调用该函数: x.foo() 不是这样的: f
我尝试使用java.time.Period,结果与我手动计算的结果相差了三天。这里奇怪的是,当我将时间段分为两个时间段时,结果与我的手动计算相符。 第二种方法就像我手动计算周期一样。 我是不是漏掉了什
我是一名优秀的程序员,十分优秀!