这是正规的网站
def transform_normal(request):
if request.method == "POST":
user_start = request.POST.get("start_user")
user_end = request.POST.get("end_user")
money = request.POST.get("money")
return HttpResponse(f"当前账户 :>>> {user_start} 向目标用户 :>>> {user_end} 转账了 :>>> {money}")
return render(request, 'transform_normal.html')
这是钓鱼的网站
def transform_normal(request):
if request.method == "POST":
user_start = request.POST.get("start_user")
user_end = request.POST.get("end_user")
money = request.POST.get("money")
return HttpResponse(f"当前账户 :>>> {user_start} 向目标用户 :>>> {user_end} 转账了 :>>> {money}")
return render(request, 'transform_normal.html')
csrf_token
csrfmiddlewaretoken
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function (xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
Cross Site Request Forgery protection | Django documentation | Django (djangoproject.com)
csrf_protect
装饰器:csrf_protect
装饰器用于需要进行CSRF保护的视图函数或类视图。csrf_protect
装饰器修饰时,Django会对该视图接收到的所有POST、PUT、DELETE等非安全HTTP方法的请求进行CSRF校验。csrf_exempt
装饰器:csrf_exempt
装饰器用于不需要进行CSRF保护的视图函数或类视图。csrf_exempt
装饰器修饰时,Django将不会对该视图接收到的任何请求进行CSRF校验。from django.views.decorators.csrf import csrf_protect, csrf_exempt
'''
csrf_protect 需要校验
csrf_exempt 忽视校验
'''
csrf校验中间件
的时候,可以在函数头上加上 @csrf_exempt
忽视校验csrf校验中间件
的时候,可以在函数头上加上 @csrf_protect
强制启动校验from django.views.decorators.csrf import csrf_protect, csrf_exempt
'''
csrf_protect 需要校验
针对 csrf_protect 符合之前的装饰器的三种用法
csrf_exempt 忽视校验
针对 csrf_exempt 只能给 dispatch 方法加才有效
'''
csrf_protect
@method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.utils.decorators import method_decorator
class MyCsrf(View):
def get(self, request):
return HttpResponse("get")
@method_decorator(csrf_protect)
def post(self, request):
return HttpResponse("post")
@method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.utils.decorators import method_decorator
@method_decorator(csrf_protect)
class MyCsrf(View):
def get(self, request):
return HttpResponse("get")
def post(self, request):
return HttpResponse("post")
dispatch
方法from django.views import View
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.utils.decorators import method_decorator
class MyCsrf(View):
@method_decorator(csrf_protect)
def dispatch(self, request, *args, **kwargs):
return super(MyCsrf, self).dispatch(request, *args, **kwargs)
def get(self, request):
return HttpResponse("get")
def post(self, request):
return HttpResponse("post")
csrf_exempt
方法dispatch
方法 有效from django.views import View
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.utils.decorators import method_decorator
class MyCsrf(View):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(MyCsrf, self).dispatch(request, *args, **kwargs)
def get(self, request):
return HttpResponse("get")
def post(self, request):
return HttpResponse("post")