参考:https://www.cnblogs.com/linxizhifeng/p/8995077.htmlhtml
阅读django CsrfViewMiddleware源码可知,csrftoken能够放在请求参数(csrfmiddlewaretoken)里面或者请求头(X-CSRFToken)里:前端
# Check non-cookie token for match. request_csrf_token = "" if request.method == "POST": try: request_csrf_token = request.POST.get('csrfmiddlewaretoken', '') except IOError: # Handle a broken connection before we've completed reading # the POST data. process_view shouldn't raise any # exceptions, so we'll ignore and serve the user a 403 # (assuming they're still listening, which they probably # aren't because of the error). pass if request_csrf_token == "": # Fall back to X-CSRFToken, to make things easier for AJAX, # and possible for PUT/DELETE. request_csrf_token = request.META.get(settings.CSRF_HEADER_NAME, '')
1 this.$http.post('/someUrl',data, [options]).then(function(response){ 2 // 响应成功回调 3 }, function(response){ 4 // 响应错误回调 5 });
vue-resource 向后端请求api, 公司的后台是用Django 开发的,Django为了防止跨站请求伪造,即csrf攻击,提供了CsrfViewMiddleware中间件来防护csrf攻击。vue
咱们在html 页面里加入{% csrf %}
来让django渲染出一个csrf的标签
(若是是form 提交表单的话,咱们要把这个标签加在form标签内,若是是用xhr提交的话写在html页面里就能够了)django
手动生成csrftoken后端
1。request.META["CSRF_COOKIE_USED"] = True
2。手动调用 csrf 中的 get_token(request) 或 rotate_token(request) 方法
3。html表单带{%csrf_token%} (适用于django render渲染的前端)api
4。装饰器ensure_csrf_cookie数组
不写在form 表单内,可是实现效果是同样的,咱们都须要在post 的表单中提供csrftoken
咱们在vue里要传送的的data 里要加上csrf的keycookie
data{ csrfmiddlewaretoken: '{{ csrf_token }}' }
这样django解析表单时会解析到csrf_token, 咱们post的数据就不会遇到403 forbidden了。app
其实这样是投机取巧的行为,这样虽然django 也能识别,可是遇到复杂的数据时就不行了,好比数组,vue-resource post 数组的时候, 由于我以前在post的option里加了一个option {emulateJSON: true},这样vue-resource 在post数据时,会把data 转换成 application/x-www-form-urlencoded
表单格式,可是这样的话,post 的数组就会被解析成arrry[0]item
这样的话,后端是不识别的,会报错。vue-resource
解决方式查到是把csrftoken 放在报头里,data 传数据,具体解决方式是加一条
Vue.http.headers.common['X-CSRFToken'] = $("input[name='csrfmiddlewaretoken']").val()
其中$("input[name='csrfmiddlewaretoken']").val()
是取django 的{% csrf %}
在模板解析后生成的input里的csrftoken。
其中报头的话django 在后台解析的时候会自动加上HTTP_
的前缀,因此说咱们的报头是 X-CSRFToken
就能够了。
另附一片参考博客地址:https://www.cnblogs.com/linxizhifeng/p/8995077.html