使用django 的cache设置token的有效期
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from api.models import Token
import datetime
from django.core.cache import cache
import pytz class LoginAuth(BaseAuthentication):
def authenticate(self, request):
'''
1 对token设置14天有效时间
2 缓存存储
:param request:
:return:
'''
# print(request.META.get("HTTP_AUTHORIZATION"))
token=request.META.get("HTTP_AUTHORIZATION")
# 1 校验是否存在token字符串
# 1.1 缓存校验
user=cache.get(token)
if user:
print("缓存校验成功")
return user,token
# 1.2 数据库校验
token_obj = Token.objects.filter(key=token).first()
if not token_obj:
raise AuthenticationFailed("认证失败!") # 2 校验是否在有效期内
print(token_obj.created) # 2018-1-1- 0 0 0
now=datetime.datetime.now() # 2018-1-12- 0 0 0
now = now.replace(tzinfo=pytz.timezone('UTC'))
print(now-token_obj.created)
delta=now - token_obj.created
state=delta < datetime.timedelta(weeks=2)
print(state)
if state:
# 校验成功,写入缓存中
print("delta",delta)
delta=datetime.timedelta(weeks=2)-delta
print(delta.total_seconds())
cache.set(token_obj.key,token_obj.user,min(delta.total_seconds(),3600*24*7))
print("数据库校验成功")
return token_obj.user,token_obj.key
else:
raise AuthenticationFailed("认证超时!")
使用django 的cache设置token的有效期的更多相关文章
- 使用Django.core.cache操作Memcached导致性能不稳定的分析过程
使用Django.core.cache操作Memcached导致性能不稳定的分析过程 最近测试一项目,用到了Nginx缓存服务,那可真是快啊!2Gb带宽都轻易耗尽. 不过Api接口无法简单使用Ngin ...
- Django 项目中设置缓存
一.配置文件settings.py中 # 设置django缓存存放位置为redis数据库,并设置一个默认(default)选项,在redis中(配置文件/etc/redis/redis.conf)开启 ...
- Django 缓存 cache基本使用
1.设置setting REDIS_HOST = '10.133.3.26' REDIS_POST = 6379 REDIS_DATABASE = 3 REDIS_PASSWORD = '' CACH ...
- Django 1.6 最佳实践: 如何设置django项目的设置(settings.py)和部署文件(requirements.txt)
Django 1.6 最佳实践: 如何设置django项目的设置(settings.py)和部署文件(requirements.txt) 作者: Desmond Chen,发布日期: 2014-05- ...
- Django分页器的设置
Django分页器的设置 有时候在页面中数据有多条时很显然需要进行分页显示,那么在python中django可以这样设置一个分页处理 怎么样去设置呢? 我们要用到 Django 中的 Pagin ...
- Django’s cache framework
小结: 1.缓存存储位置:数据库.文件系统.内存 2.通过缓存前缀实现跨服务器缓存 Django’s cache framework | Django documentation | Django h ...
- django的mysql设置和mysql服务器闲置时间设置
服务器启动后,每个进程都会主动连接到mysql,要是长时间没有数据交互,mysql会自动断开连接. show variables like '%timeout%'; 闲置连接的超时时间由wait_t ...
- django的cache
使用文件缓存 #settings.py CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.filebased.F ...
- ArcGIS Silverlight 设置token
背景 arcgis for server采用多种安全认证方式.常用的就是就是采用token机制.所以对服务设置了安全,则前端需要提供相对应的token凭证.通常来说设置token有以下两种情形: 一是 ...
随机推荐
- postgresql源码编译安装(centos)
centos6.8安装postgresql-9.6.8 一.环境 centos6.8 postgresql-9.6.8 二.准备工作 虚拟机可以连接外网 三.先安装make,gcc,gcc-c++,r ...
- WAMPSERVER php
The Apache service named reported the following error:>>> (OS 10013)An attempt was made to ...
- MacOS X 安装OpenCV3.2
windows平台和linux平台安装参见 官方文档:http://docs.opencv.org/3.2.0/da/df6/tutorial_py_table_of_contents_setup.h ...
- C# WPF 4.5 RibbonWindow
WPF RibbonWindow , Z .Net4.5里有自带的RibbonWindow,需要引用 System.Windows.Controls.Ribbon.dll 题外话:自带的Ribbon ...
- 008-Spring Boot @EnableAutoConfiguration深入分析、内部如何使用EnableAutoConfiguration
一.EnableAutoConfiguration 1.EnableAutoConfiguration原理 springboot程序入口使用注解@SpringBootApplication,Sprin ...
- django-xadmin常用内容记录
自定义菜单名称: 1 修改app下的 apps.py文件 添加 class OperationConfig(AppConfig): name = 'operation' verbose_name = ...
- ecshop后台增加模块菜单详细教程
我们有时候针对ecshop如此开发,想在后台加一些菜单,最模板以前提供过教程,但是并非很系统,今天最模板抛砖引玉图文教程告诉大家:如何在ecshop后台增加模块菜单! 首先需要修改四个文件:inc_p ...
- failed to open stream: HTTP request failed! HTTP/1.1 505 HTTP Version Not Supported
PHP模拟GET请求支付宝退款链接(未用sdk 自己拼接的请求链接与参数); 起初用file_get_contents();就报错 :failed to open stream: HTTP requ ...
- python每日一练:0004题
第 0004 题: 任一个英文的纯文本文件,统计其中的单词出现的个数. import re count = 0 with open('./EnglishText.txt','r') as f: tem ...
- Queen Attack -- 微软2017年预科生计划在线编程笔试第二场
#!/usr/bin/env python # coding:utf-8 # Queen Attack # https://hihocoder.com/problemset/problem/1497 ...