自定义token,保存到客户端的cookie中,
自定义token
#原理自定义token,放入cookie中,不用存数据库
#token定义方式 >>>>> "加密字符串"|登陆用户id|用户登陆时间
#加密字符串由登陆用户id,登陆时间和盐通过md5加密完成
import hashlib
def get_token(user_id,current_time):
md5= hashlib.md5()
md5.update("宝塔镇河妖".encode("utf-8"))
md5.update(str(current_time).encode("utf-8"))
md5.update(str(user_id).encode("utf-8"))
md5.update("egon掏大刀".encode("utf-8"))
token ="|".join([md5.hexdigest(),str(user_id),str(current_time)])
return token
#对应的解密方法
def check_token(token,redis_conn):
try:
res = redis_conn.get(token)
if not res:
return False,"未登陆"
user_info = token.split("|")
user_id = user_info[1]
create_time = user_info[2]
if token != get_token(user_id,create_time):
return False,"非法登陆"
return True,"登陆成功"
except Exception as e:
print(e)
return False,"未知错误"
pass
#登陆函数
def post(self, request):
uname = request.POST.get("uname")
user = User.objects.filter(uname=uname)
if not user:
return Response({"status": 101, "msg": "user not exists"})
pwd = request.POST.get("pwd")
hashlib_pwd = hash_pwd(pwd)
db_pwd = user[0].pwd
if hashlib_pwd != db_pwd:
return Response({"status": 102, "msg": "password error"})
try:
token = get_token(user[0].pk, time.time())
if user[0].isadmin:
response = render(request, "admin/index.html", {"uname": uname})
else:
response = render(request, "user/index.html", {"uname": uname})
#将token信息放入cookie中,客户端就会将token存入cookie中,下次来的时候request.COOKIE.get("token")就能拿到
response.set_cookie("token", token)
return response
except Exception as e:
return Response({"status": 103, "msg": "unknown error"})
自定义token,保存到客户端的cookie中,的更多相关文章
- Request对象主要用于获取来自客户端的数据,如用户填入表单的数据、保存在客户端的Cookie等。
1.主要属性 ApplicationPath 获取服务器上asp.net应用程序的虚拟应用程序根路径 Browser 获取有关正在请求的客户端的浏览器功能的信息,该属性值为:HttpBrows ...
- Express 项目,res.cookie() 设置 Cookie 无法被保存在浏览器的 Application 中
res.cookie() 给客户端响应头封装的 Cookie 无法被保存在客户端浏览器的 Application 中,只能在 Set-Cookie 中看到有这个值: 在前后端分离项目中,存在跨域问题, ...
- java web实现在cookie中保存用户名和密码,用户自动登入
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- JSON WEB TOKEN,简单谈谈TOKEN的使用及在C#中的实现
十年河东,十年河西,莫欺少年穷. 学无止境,精益求精. 突然发现整个十月份自己还没有写一篇博客......哎,说出来都是泪啊,最近加班实在实在实在是太多了,真的没有多余的时间写博客.这不,今天也在加班 ...
- Cookie的使用、Cookie详解、HTTP cookies 详解、获取cookie的方法、客户端获取Cookie、深入解析cookie
Cookie是指某些网站为了辨别用户身份.进行session跟踪而存储在用户本地终端上的数据(通常经过加密),比如说有些网站需要登录才能访问某个页面,在登录之前,你想抓取某个页面内容是不允许的.那么我 ...
- Cookie中的sessionid与JSONP原理
一.首先说明一下cookie中的sessionid的作用. 1.cookie只是一些文本内容,多是键值对的形式,是请求头中的一部分 2.http是无连接的 知道这两点,就可以很容易的理解session ...
- 如果客户端禁用cookie,session还能使用吗?
记得在以前找工作的时候,可多次被问到如果客户端被禁用cookie,session还能使用吗? 今天终于找到了相关的答案:我们来看一下: session是在服务器段保持会话数据的一种方法,对应的cook ...
- Servlet课程0426(九)Servlet服务器端创建Cookie和客户端读取Cookie
服务器端创建Cookie: Win7默认Cookie位置 C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Cookies Cookie ...
- c#登录时保存账号密码到cookie
登陆界面有用户名.密码输入框,一个’记住账号密码‘的复选框. 1.登录时,勾选‘记住账号密码‘复选框,则会把用户名密码保存在客户端cookie里,保存时间为最大值(直到用户清除浏览器缓存或者取消勾选’ ...
随机推荐
- 【计算机网络】ssl双向认证和单向认证原理
有朋友在搞一个项目,周末有聊到一些安全性的东西,很自然会想起https,但https究竟如何实施,其原理又是什么? 基于ssl,一般的应用都是单向认证,如果应用场景要求对客户来源做验证也可以实现成双向 ...
- Linux安装配置相关
1.常用汇总 useradd usertemp //添加用户 passwd usertemp //修改/设置密码 userdel usertemp //删除用户 2. profile/baserc等配 ...
- Http中常见MIME类型
MIME类型 常见MIME类型: 超文本标记语言文本 .html text/html xml文档 .xml text/xml XHTML文档 .xhtml application/xhtml+xml ...
- PyQt学习笔记
---------------个人学习笔记--------------- 1.QtWidgets.QApplication.instance().quit() 方法可退出当前窗体 2.self.Qla ...
- 有趣的 验证JS只能输入正整数
<html> <head> <title>只能输入正整数</title> </head> <body> 兑换数量:<inp ...
- iOS开发ReactiveCocoa学习笔记(六)
RAC操作方法三. demo地址:https://github.com/SummerHH/ReactiveCocoa.git doNext deliverOn timeout interval del ...
- springboot 学习笔记(六)
(六)springboot整合activemq 1.现下载activemq,下载链接:http://activemq.apache.org/download.html,windows系统解压后进入bi ...
- Diary
2019.1.16 233333333 2018.12.5 猜猜我写了什么? 2018.12.3 maya真是越来粤菜了.. 突然发现cf其实有中文 2018.12.1 说好的今天出成绩呢?.. 咕咕 ...
- ios微信浏览器音乐自动播放
setTimeout(function(){ //一般情况下,这样就可以自动播放了,但是一些奇葩iPhone机不可以 document.getElementById('bgmedia').play() ...
- centos 安装 freeswitch,开启与关闭
---恢复内容开始--- 官网说明地址 :https://freeswitch.org/confluence/display/FREESWITCH/CentOS+7+and+RHEL+7 1.获取源码 ...