CSRF Failed: Referer checking failed - no Referer
postman模拟登录出了这个错误,其实看标题就知道大概是怎么回事,网上大概找了办法,也没说到位,所以干脆自己找源码了.
问题很明显就是出在 CSRF 上,理所当然去查看 CsrfViewMiddleware 中间件:
class CsrfViewMiddleware(object): def _accept(self, request): request.csrf_processing_done = True
return None def _reject(self, request, reason):
logger.warning('Forbidden (%s): %s', reason, request.path,
extra={
'status_code': 403,
'request': request,
}
)
return _get_failure_view()(request, reason=reason) def process_view(self, request, callback, callback_args, callback_kwargs): if getattr(request, 'csrf_processing_done', False):
return None try:
csrf_token = _sanitize_token(
request.COOKIES[settings.CSRF_COOKIE_NAME])
request.META['CSRF_COOKIE'] = csrf_token
except KeyError:
csrf_token = None request.META["CSRF_COOKIE"] = _get_new_csrf_key() return None
e that anything not defined as 'safe' by RFC2616 needs protection
if request.method not in ('GET', 'HEAD', 'OPTIONS', 'TRACE'):
if getattr(request, '_dont_enforce_csrf_checks', False):
return self._accept(request) if request.is_secure():
referer = force_text(
request.META.get('HTTP_REFERER'),
strings_only=True,
errors='replace'
)
if referer is None:
return self._reject(request, REASON_NO_REFERER) # 问题就出在这里 good_referer = 'https://%s/' % request.get_host()
if not same_origin(referer, good_referer):
reason = REASON_BAD_REFERER % (referer, good_referer)
return self._reject(request, reason) if csrf_token is None:
return self._reject(request, REASON_NO_CSRF_COOKIE)
request_csrf_token = ""
if request.method == "POST":
try:
request_csrf_token = request.POST.get('csrfmiddlewaretoken', '')
except IOError:
pass if request_csrf_token == "":
request_csrf_token = request.META.get('HTTP_X_CSRFTOKEN', '') if not constant_time_compare(request_csrf_token, csrf_token):
return self._reject(request, REASON_BAD_TOKEN) return self._accept(request)
首先在文件里面搜索报错的信息字符串,找到这个:
REASON_NO_REFERER = "Referer checking failed - no Referer."
然后搜索 REASON_NO_REFERER 就会找到上面的那一段代码中出错的位置.(为什么写这些东西,因为我有个同学就是从来都不会找出错原因,方法有时候比结论更重要)
然后代码里面说的很清楚了, referer = request.META.get('HTTP_REFERER')
这就说明请求头里面没有 REFERER这个信息,都知道的,Django会把自定义的请求头大写并在前面添加 HTTP_ ,所以在postman的header里面添加这个 referer 字段就好了.
注意,referer的格式是 https://www.domain.com/. 后面有个点
然后就OK了
多说两句,看看人家django多贴心,如果是post请求的话,会尝试从两个地方获取csrftoken,请求体里面可以放,就算忘了放,还会从请求头里面拿(54行 - 64行)
CSRF Failed: Referer checking failed - no Referer的更多相关文章
- 【转】Memory gates checking failed because the free memory***解决办法
Issue: Vault users cannot connect. VLOGS show the following error: Memory gates checking failed beca ...
- [bug]WCF 内存入口检查失败 Memory gates checking failed
bug描述 异常信息:内存入口检查失败,因为可用内存(xxx 字节)少于总内存的 xx%.因此,该服务不可用于传入的请求.若要解决此问题,请减少计算机上的负载,或调整 serviceHostingEn ...
- WebHost failed to process a request.Memory gates checking failed because the free memory (140656640 bytes) is less than 5% of total memory
WebHost failed to process a request. Sender Information: System.ServiceModel.ServiceHostingEnvironme ...
- tabpagerindictor:mergeReleaseResources FAILED Error:Execution failed for task ':tabpagerindictor:mergeReleaseResources'. > D:\android\adt-bundle-windows-x86_64-20140702\android-open-project-demo-mast
解决:将项目移动到路径少的目录再运行即可 异常日志: D:\android\adt-bundle-windows-x86_64-20140702\android-open-project-demo-m ...
- 解决Installation failed with message Failed to finalize session : INSTALL_FAILED_INVALID_APK的问题
Android Studio 运行AVD的时候出现: Installation failed with message Failed to finalize session : INSTALL_FAI ...
- 安装APK时报 Installation failed with message Failed to finalize session : INSTALL_FAILED_USER_RESTRICTED: Invalid apk.
Installation failed with message Failed to finalize session : INSTALL_FAILED_USER_RESTRICTED: Invali ...
- 小米系列手机调试Installation failed with message Failed to establish session
用Android studio 2.3调度程序时提示"Installation failed with message Failed to establish session"错误 ...
- WCF 内存入口检查失败 Memory gates checking failed
在做JC系统时,出现这样的错误: 出现该错误信息的原因是因为WCF服务激活之前,系统应该具有的最小内存量不足config文件中设置的百分比.我是在本机调试的时候出现的. 解决方法: 关闭 ...
- org.springframework.beans.factory.BeanCreationException,Invocation of init method failed,Context initialization failed
G:\javaanzhuang\apache-tomcat-\bin\catalina.bat run [-- ::,] Artifact ssm_qingmu02_web:war exploded: ...
随机推荐
- 从Linux系统内存逐步认识Android应用内存
总述 Android应用程序被限制了内存使用上限,一般为16M或24M(具体看系统设置),当应用的使用内存超过这个上限时,就会被系统认为内存泄漏,被kill掉.所以在Android开发时,管理好内存的 ...
- 关于nios 中printf 的问题
在nios中,有printf的程序,在线调试没有什么问题,但是下到flash里面,程序跑了一段时间就死掉了!JTAG_UART是阻塞式输出,他只是将数据输出到buffer中,等待你上位机读取,当你的b ...
- 30.angularJS第一个实例
转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 通过 ng-directives 扩展了 HTML. ng-app 指令定义一个 Angu ...
- POJ 3256 DFS水题
枚举点 每次都搜一遍 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> ...
- cors跨域的前端实现---根据资料整合的
1.服务端 搁response中增加Access-Control-Allow-Origin:‘*’ eg: context.Response.AddHeader("Access-Contr ...
- 小试VS 2017 开发Python Django项目过程一
一.新建项目python ->django web 项目 (选择带bootstrap风格与twwriter)项目名称iepiececomputing (ie计件计算)跳出窗体 -> 添加虚 ...
- 【安卓】数据库基于脚本的"增量更新",每次更新时不需改动java代码、!
思路: 1.当然是基于SQLiteOpenHelper.onCreate(第一次安装程序时调用).onUpdate(升级程序时调用) 2.用"脚本"(脚本制作详细方法问度娘)做数据 ...
- eXtremeDB相关问题解答(3)
> 1. Could our database support multi-database under one single instance? > > 2. ...
- js---10作用域链
<html> <head> <title>scope basic</title> </head> <body> <scri ...
- 暑假集训-二分图,网络流,2-SAT
匈牙利算法DFS bool dfs(int u){ ; i <= n; i++){ if(a[u][i] && !visit[i]){ visit[i] = true; || d ...