cookie

cookie是保存客户端浏览器上的键值对,是服务端设置在客户端浏览器上的键值对,也就意味着浏览器其实可以拒绝服务端的‘命令’,默认情况下浏览器都是直接让服务端设置键值对

设置cookie
obj1.set_cookie()
获取cookie
request.COOKIES.get()
删除cookie
obj1.delete_cookie()

cookie实现登陆功能

from functools import wraps
def login_auth(func):
@wraps(func)
def inner( request,*args,**kwargs):
if request.COOKIES.get('username'):
res=func( request,*args,**kwargs)
return res
else:
target_url=request.path_info ###获取到网页的url
return redirect('/login/?next=%s'%target_url)
return inner def login(request):
if request.method=='POST':
username = request.POST.get('username')
password = request.POST.get('password')
if username == 'jason' and password == '123':
old_path = request.GET.get('next')
if old_path:
# 保存用户登录状态
obj = redirect(old_path)
else:
obj = redirect('/home/')
obj.set_cookie('name', 'lzs') # 让客户端浏览器 记录一个键值对
# obj.set_cookie('name','jason',max_age=5) # 让客户端浏览器 记录一个键值对
return obj
return render(request, 'login.html') @login_auth
def home(request):
# if request.COOKIES.get('name'):
# return HttpResponse('我是主页,只有登陆了才能看')
# return redirect('/login/')
return HttpResponse('我是主页,只要登录了才能看') @login_auth
def index(request):
return HttpResponse('我是index')

session

session是保存在服务器上的键值对,django session默认的过期时间是14天

设置session
request.session['key']=value 仅仅只会在内存产生一个缓存
1、django内部自动生成了随机的字符串
2、在django_session表中存入数据
3、将产生的随机的字符串发送给浏览器,让浏览器保存到cookie中 获取session
request.seesion.get('key')
1、浏览器发送cookie到django_session表中对比,是否有对应的数据
2、拿着随机字符串去django_session表中比对,取出赋值给request.session,
如果对不上那么request.session就是个空
(django session表是针对浏览器的,不同的浏览器来,才会有不同额记录)
删除session
request.session.delete() #只删除服务端的session
request.session.flush() #浏览器和服务端全部删除

session也可以设置超时时间

​ request.session.set_expiry(value 多种配置)

​ 数字、0、不写、时间格式

from functools import wraps

def login_auth(func):
@wraps(func)
def inner(request,*args,**kwargs):
if request.session.get('username'):
res = func(request,*args, **kwargs)
return res
else:
target_url=request.path_info
return redirect('/login/?next=%s'%target_url)
return inner def login(request):
if request.method=='POST':
username=request.POST.get('username')
password=request.POST.get('password')
old_path = request.GET.get('next')
print(old_path)
if username=='lzs' and password=='123':
request.session['username'] = username
if old_path:
obj= redirect(old_path)
else:
return redirect('/home/')
# obj.set_cookie('name', 'lzs')
return render(request,'login.html')

django中间件

  • 用户访问频率限制
  • 用户是否是黑名单
  • 所有用户登录校验
  • 只要涉及到网址全局的功能就该使用中间件
django中间件暴露给程序员五个可以自定义的方法(五个方法都是在特定的条件下自动触发的)
1、新建一个文件夹,里面新建一个任意名称的py文件,里面写类,固定继承类
from django.utils.deprecation import MiddlewreMixin
class MyMiddle(MiddlewareMixin):
2、去配置文件注册到中间件中,手写字符串的路径
'app01.mymiddleware.myaabb.MyMiddle1'
需要掌握:
process_request():
请求来的时候会从上往下经过每一个中间件里面,process_request一旦里面返回了
HttpResponse对象那么就不再往后执行了,会执行同一级别
eg:def process_request(self,request):
print('我是第一个自定义中间件里面的process_request方法')
return HttpResponse("我是第一个自定义中间件里面的HttpResponse对象返回值")
process_response():响应走的时候会从下往上依次经过每一个中间件里面process_response
eg:def process_response(self,request,response):
# response就是要返回给用户的数据
print("我是第一个自定义中间件里面的process_response方法")
return response
了解:
process_view:路由匹配成功之后执行视图函数之前触发
process_exception:当视图函数出现异常(bug)的时候自动触发
process_template_response:当视图函数执行完毕之后并且返回的对象中含有render方法的情况下才会触发
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse class MyMiddle1(MiddlewareMixin):
def process_request(self,request):
print('我是第一个自定义中间件里面的process_request方法')
# return HttpResponse("我是第一个自定义中间件里面的HttpResponse对象返回值") def process_response(self,request,response): # response就是要返回给用户的数据
print("我是第一个自定义中间件里面的process_response方法")
return response def process_view(self,request,view_func,view_args,view_kwargs):
print(view_func,view_args,view_kwargs)
print('我是第一个自定义中间件里面的process_view方法') def process_exception(self,request,exception):
print(exception)
print('我是第一个自定义中间件里面的process_exception方法') def process_template_response(self,request,response):
print('我是第一个自定义中间件里面的process_template_response方法')
return response class MyMiddle2(MiddlewareMixin):
def process_request(self,request):
print('我是第二个自定义中间件里面的process_request方法') def process_response(self, request, response): # response就是要返回给用户的数据
print("我是第二个自定义中间件里面的process_response方法")
return response def process_view(self,request,view_func,view_args,view_kwargs):
print(view_func,view_args,view_kwargs)
print('我是第二个自定义中间件里面的process_view方法') def process_exception(self,request,exception):
print(exception)
print('我是第二个自定义中间件里面的process_exception方法') def process_template_response(self,request,response):
print('我是第二个自定义中间件里面的process_template_response方法')
return response

跨站请求伪造(csrf)

就类似你搭建了一个跟银行一模一样的web页面,用户在你的网站转账的时候输入用户名、密码、对方账户,银行里面的钱确实少了,但是发现收款人变了

最简单的原理:
你写的form表单中用户的用户名、密码都会真实的提交给银行后台,但是收款账户却不是用户填的,你暴露给用户的是一个没有name属性的input框,你自己提前写好了一个带有name和value的input框 解决钓鱼网站的策略:
只要是用户想要提交post请求的页面,在返回给用户的时候提前设置好一个随机字符串,当用户提交post请求的时候,自动先查取是否有该随机字符串,如果有正常提交,没有直接报403
  • form表单:

你在写的时候只需加上一个{% csrf_token %}

  • ajax:

  • 第一种自己在页面上先通过{% csrf_token %}获取随机字符串,然后利用标签查找

    data:{'username':'jason','csrfmiddlewaretoken'

    cookie、session以及中间件的更多相关文章

    1. cookie与session django中间件

      目录 一.什么是cookie 二.django中操作cookie 2.1 如何操作cookie 2.2 操作cookie 三.什么是session 四.django中操作session 4.1 创建c ...

    2. 1204 中间件以及cookie,session

      目录 一 .cookie与session原理 1.cookie 操作 1.1 设置cookie set_cookie 1.2 获取cookie request.COOKIES.get('k1') 1. ...

    3. cookie、session和中间件

      目录 cookie和session cookie与session原理 cookie Google浏览器查看cookie Django操作cookie 获取cookie 设置cookie 删除cooki ...

    4. Django框架-cookie和session以及中间件

      目录 一.cookie 和 session 1.为什么会有这些技术 2. cookie 2.1 Django如何设置cookie 2.2 Django如何获取cookie 2.3 Django如何设置 ...

    5. day13 cookie与session和中间件

      day13 cookie与session和中间件 今日内容概要 cookie与session简介 django操作cookie与session django中间件简介 如何自定义中间件 csrf跨站请 ...

    6. Cookie Session和自定义分页

      cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

    7. python自动化开发-[第十九天]-分页,cookie,session

      今日概要: 1.cookie和session 2.csrf 跨站请求伪造 3.自定义分页 一.上节回顾: http请求的本质: - Http请求本质 浏览器(socket客户端): 2. socket ...

    8. day70 cookie & session 前后端交互分页显示

      本文转载自qimi博客,cnblog.liwenzhou.com 概要: 我们的cookie是保存在浏览器中的键值对 为什么要有cookie? 我们在访问浏览器的时候,千万个人访问同一个页面,我们只要 ...

    9. node.js cookie session使用教程

      众所周知,HTTP 是一个无状态协议,所以客户端每次发出请求时,下一次请求无法得知上一次请求所包含的状态数据,如何能把一个用户的状态数据关联起来呢? cookie 首先产生了 cookie 这门技术来 ...

    10. Cookie Session 和Django分页

      cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

    随机推荐

    1. BZOJ3680 JSOI2004 平衡点 - 随机/近似算法

      迭代乱搞了下就过了…… #include <bits/stdc++.h> using namespace std; ],y[],w[]; double xm,ym,wt,k,lambda= ...

    2. navicat 连接报2059错误

      原因 navicat不支持mysql新版本的加密规则,mysql8 之前的版本中加密规则是mysql_native_password, mysql8之后,加密规则是caching_sha2_passw ...

    3. google protocol 入门 demo

      ubunbu系统下google protobuf的安装 说明: 使用protobuf时需要安装两部分: 第一部分为*.proto文件的编译器,它负责把定义的*.proto文件生成对应的c++类的.h和 ...

    4. docker镜像相关的常用操作

      1.保存镜像 #docker save 镜像名称 -o 保存的完整地址和文件名 docker save zhoushiya/zhiboyuan -o d:/zhiboyuan.tar 2.载入镜像 # ...

    5. 【算法学习记录-排序题】【PAT A1025】PAT Ranking

      Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhe ...

    6. Copy Paste DWG to older 3ds Max

      Hi, This is quick tutorial: how to install Auto Cad scripts to be able to copy from newer Auto Cad t ...

    7. python之路set

      一.set和其他集合的区别: list :允许重复的集合,修改 tuple:允许重复的集合,不修改 dict:字典 set:不允许重复的集合,set不允许重复的,列表是无序的 1.创建一个set s= ...

    8. 巨杉TechDay回顾 | WARNING!您参加的数据库沙龙热度已爆表……

      自从2008年“大数据”这一概念被首次提出以来,在过去这10年中,几乎各行各业都或多或少受到了这一概念的影响.与此同时,在AI.云计算.物联网.区块链等新兴技术快速发展的今天,数据库己经成为了决定所有 ...

    9. [bzoj1905] [ZJOI2007] Hide 捉迷藏

      题意简述 给定一棵 \(n\) 个点的树,起初每个点都为黑色. 2种操作,要么改变某个点的颜色(由黑至白或由白至黑),要么询问距离最远的两个黑点间的距离. 共 \(m\) 次操作. \(n\leq 1 ...

    10. nginx的配置总结,有时间自己整理

      推荐文章:https://www.cnblogs.com/digdeep/p/4217310.html