- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在 Axios.interceptors.request
的配置中获取 Cookies 的 csrftoken
?
Axios.interceptors.request.use(
config => {
if (
config.method === "post" ||
config.method === "put" ||
config.method === "delete"||
config.method === "get"
) {
}
if (Cookies.get('token')!==undefined) {
config.headers['Authorization']= 'Token '+Cookies.get('token');
}
// there I try to get the `csrftoken` in the Cookies, but I can not get.
if (Cookies.get('csrftoken')!==undefined) {
config.headers['x-csrftoken']= Cookies.get('csrftoken'); // 'CSRFToken'
}
return config;
},
error => {
return Promise.reject(error.data.error.message);
}
);
在Axios.interceptors.request
的配置中我无法获取Cookies的csrftoken
:Cookies.get('csrftoken')
。
我的 AxiosConfig 代码如下:
AxiosConfig:{
baseURL: 'http://10.10.10.105:8001/',
responseType: "json",
withCredentials: true, // there will send the Cookie (with it there are: sessionid, csrftoken)
xsrfCookieName: 'csrftoken', // default: XSRF-TOKEN
xsrfHeaderName: 'x-csrftoken', // default: X-XSRF-TOKEN
headers: {
"Content-Type": "application/json;charset=utf-8"
}
}
edit-1
Cookie中有csrftoken
edit-2
并且在cookie中,也没有csrftoken
。
edit-3
如果我在控制台中获得 document.cookie
,我将获得 ""
:```
document.cookie
< "" ```
edit-4
在我的 Django 后端,settings.py
:
INSTALLED_APPS:
...
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
'rest_framework_docs',
'rest_auth',
'allauth',
'allauth.account',
'allauth.socialaccount',
'rest_auth.registration',
...
我不确定rest_auth
和allauth
是否会影响csrftoken
。
最佳答案
首先,始终确保 cookie 未被标记为 httpOnly .
如果是,您的 javascript
代码将无法读取/修改其内容。
您可以检查浏览器中的 cookies 选项卡,您会看到它是否可读。
但在您的情况下,django
不应将标志设置为 httpOnly
作为 docs描述如何直接从 cookie 中读取 javascript
中的值。
我可以根据经验指出一些事情:
config.headers = ...;
可能会触发错误。 确保你写:
config.headers = config.headers || {};
在设置 headers
之前,不会触发 'config.headers is undefined'。
csrf
值存储在隐藏的 input
中。你可以这样做(语法可能不正确):
<input type="hidden"
name="csrftoken"
value="{% csrf_token %}"/>
</div>
然后在拦截器中发送 token :
config.headers['x-csrftoken'] = document.querySelector('input[name="csrftoken"]').value;
在这种情况下,由于您不需要读取 cookie,将其设置为 httpOnly
将是一个巨大的优势。
关于javascript - 如何在 `csrftoken` 的配置中获取 Cookies 的 `Axios.interceptors.request`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49312473/
我正在执行命令 ng g interceptor error 其中错误是拦截器的名称 但面临如下问题: An unhandled exception occurred: Schematic "inte
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是《quarkus依赖注入》系列的第五篇
我在每个 Controller 代码(例如@UseInterceptors(ClassSerializerInterceptor))中都使用了,所以我决定将其设置为全局,并且尝试设置它时不走运。 我在
应用程序开发语言:ionic + Restangular 我在 Restangular 中有一个用于 BaseUrl 和 Auth Interceptor 的全局配置。[就像 http://app 一
在我的服务类中,我需要可用的 hibernate session 。我目前在 beans.xml 中这样做: com.app.dao.UserDao
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码): https://github.com/zq2599/blog_demos 本篇概览 本
mybatis采用责任链模式,通过动态代理组织多个拦截器(插件),通过这些拦截器可以改变mybatis的默认行为(诸如sql重写之类的),由于插件会深入到mybatis的核心,因此在编写自己的插件前
下面给大家介绍mybatis interceptor 处理查询参数及查询结果,具体代码如下所示: ?
我一直在尝试实现应用程序范围的拦截器,但是,我一直看到以下错误: TypeError: this.interceptor.intercept is not a function 经过多次调试,我意识到
我有一个 HTTP 拦截器。后端 API 需要自定义 header ,其值在应用初始化和其他操作期间存储在 indexdb 中。因此,我需要获取值并在每个请求中传递它。但到目前为止,未能这样做,因为拦
我的 HibernateInterceptor 有问题。我延长了 EmptyInterceptor并覆盖 onFlushDirty方法。 我使用这种方法手动检查脏值(将以前的值与当前值进行比较)来为更
我在 Angular 5 中有一个有效的拦截器。它在 App.module 的提供者中注册,并正确拦截了从应用程序发出的所有请求。 问题是它不会拦截从应用程序使用的库发出的请求。 我正在使用开源库 (
我们已经实现了以下内容 function Inteceptors($httpProvider) { 'ng-inject'; $httpProvider.interceptors.pu
我需要拦截请求/响应,类似于 java 过滤器中的处理方式。在 Django 中执行此操作的最佳方法是什么?我只是要创建自己的中间件还是已经存在其他东西? 最佳答案 如果您需要拦截客户端请求/响应,请
目前,我们在 Jboss EAP 6.2 上使用 CXF 2.7.3 时遇到问题,并出现自定义 SoapFault 异常。 当我们发送自定义 SoapFault 时,不会显示子代码及其值: 这是我们想
CDI 中拦截器的范围是什么? aka,这合法吗?我会在每个调用拦截器的地方获得相同的拦截器实例吗? @RequestScoped public class SalesForceControllerI
我正在构建一个有 2 个拦截器的 angular 5 应用程序: 重试失败的 504 请求 另一个向用户显示有关失败请求的错误消息 我希望仅当错误不是 504 或错误为 504 并且已被第一个拦截器重
利用Spring MVC 的 Interceptor 实现个简易的性能监控,计算一下每个url的执行时间。 在 preHandle 方法中记录当前的时间戳到线程局部变量里,然后在afterComp
我想知道是否有办法在 Angular 的 HttpInterceptor 中检索当前路由。我正在使用来自不同路由的相同拦截器,但我需要检查是否正在从特定路由使用拦截器。如果是这样,我想在执行之前向后端
我创建了一个简单的拦截器来将用户重定向到登录页面,服务器返回 401 错误。但是它不起作用,因为状态被设置为 404,所以 $location 永远不会被调用。 .config( function m
我是一名优秀的程序员,十分优秀!