我有一个涉及的设置
前端服务器(Node.js,域:localhost:3000)<---> 后端(Django,Ajax,域:localhost:8000)
浏览器 <-- webapp <-- Node.js(服务应用程序)
浏览器 (webapp) --> Ajax --> Django(服务 ajax POST 请求)
现在,我的问题是 webapp 用于对后端服务器进行 Ajax 调用的 CORS 设置。在 chrome 中,我不断得到
当凭证标志为真时,不能在 Access-Control-Allow-Origin 中使用通配符。
也不适用于Firefox。
我的 Node.js 设置是:
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:8000/');
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
};
在 Django 中,我使用的是 this middleware along with this
webapp 发出这样的请求:
$.ajax({
type: "POST",
url: 'http://localhost:8000/blah',
data: {},
xhrFields: {
withCredentials: true
},
crossDomain: true,
dataType: 'json',
success: successHandler
});
因此,webapp 发送的请求标头如下所示:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: "Origin, X-Requested-With, Content-Type, Accept"
Access-Control-Allow-Methods: 'GET,PUT,POST,DELETE'
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: csrftoken=***; sessionid="***"
这是响应标头:
Access-Control-Allow-Headers: Content-Type,*
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETE
Content-Type: application/json
我哪里错了?!
编辑 1:我一直在使用 chrome --disable-web-security
,但现在希望事情能够真正发挥作用。
编辑 2: 答案:
所以,我的解决方案 django-cors-headers
配置:
CORS_ORIGIN_ALLOW_ALL = False
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = (
'http://localhost:3000' # Here was the problem indeed and it has to be http://localhost:3000, not http://localhost:3000/
)
http
,而是最后的 /
。我想省略 http 可能会起作用,但是我已经有几年没有真正研究过这个东西了,所以现在真的不知道什么有效!
这是安全的一部分,你不能这样做。如果您想允许凭据,那么您的 Access-Control-Allow-Origin
不得使用 *
。您必须指定确切的协议 + 域 + 端口。供参考,请参阅以下问题:
Access-Control-Allow-Origin 通配符子域、端口和协议 使用凭证的跨域资源共享
此外 *
过于宽松,会破坏凭据的使用。因此将 http://localhost:3000
或 http://localhost:8000
设置为允许来源标头。
如果您正在使用 CORS 中间件并且想要发送 withCredential
boolean true,则可以像这样配置 CORS:
var cors = require('cors');
app.use(cors({credentials: true, origin: 'http://localhost:3000'}));
扩展@Renaud 的想法,cors 现在提供了一种非常简单的方法:
从 cors 官方文档中找到 here:
" origin:配置 Access-Control-Allow-Origin CORS 标头。可能的值:布尔值 - 将 origin 设置为 true 以反映请求来源,如 req.header('Origin') 所定义,或将其设置为 false 以禁用 CORS 。”
因此,我们只需执行以下操作:
const app = express();
const corsConfig = {
credentials: true,
origin: true,
};
app.use(cors(corsConfig));
最后,我认为值得一提的是,在某些用例中,我们希望允许来自任何人的跨源请求;例如,在构建公共 REST API 时。
如果您使用 express
,您可以使用 cors 包来允许这样的 CORS,而不是编写中间件;
var express = require('express')
, cors = require('cors')
, app = express();
app.use(cors());
app.get(function(req,res){
res.send('hello');
});
app.use(cors({credentials: true}));
django-cors-header
应用程序。确保将 localhost 添加到 CORS_ORIGIN_WHITELIST
设置并将 CORS_ALLOW_CREDENTIALS
设置为 True
CORS_ORIGIN_ALLOW_ALL = True
、CORS_ORIGIN_WHITELIST = ( 'localhost' )
和 CORS_ALLOW_CREDENTIALS = True
我得到了这些标题:Access-Control-Allow-Credentials: true Access-Control-Allow-Origin: http://localhost:3000/ Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETE Content-Type: application/json
试试看:
const cors = require('cors')
const corsOptions = {
origin: 'http://localhost:4200',
credentials: true,
}
app.use(cors(corsOptions));
如果您想允许所有来源并保持凭据真实,这对我有用:
app.use(cors({
origin: function(origin, callback){
return callback(null, true);
},
optionsSuccessStatus: 200,
credentials: true
}));
这在开发中对我有用,但我不能建议在生产中,这只是完成工作的另一种方式,尚未提及但可能不是最好的。无论如何,这里是:
您可以从请求中获取来源,然后在响应标头中使用它。这是它在快递中的样子:
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', req.header('origin') );
next();
});
我不知道你的 python 设置会是什么样子,但这应该很容易翻译。
(编辑)以前推荐的插件不再可用,您可以尝试 this other one
为了在 Chrome 中进行开发,安装 this add on 将消除该特定错误:
Access to XMLHttpRequest at 'http://192.168.1.42:8080/sockjs-node/info?t=1546163388687'
from origin 'http://localhost:8080' has been blocked by CORS policy: The value of the
'Access-Control-Allow-Origin' header in the response must not be the wildcard '*'
when the request's credentials mode is 'include'. The credentials mode of requests
initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
安装后,请确保通过单击插件(CORS,绿色或红色)图标并填写相应的文本框,将您的 url 模式添加到 Intercepted URLs
。要在此处添加的适用于 http://localhost:8080
的示例 URL 模式是:*://*
在执行请求之前,使用 auth 拦截器编辑标头有这个问题。我们使用 api-token 进行身份验证,所以我启用了凭据。现在,似乎不再需要/允许
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
req = req.clone({
//withCredentials: true, //not needed anymore
setHeaders: {
'Content-Type' : 'application/json',
'API-TOKEN' : 'xxx'
},
});
return next.handle(req);
}
除此之外,目前没有任何副作用。
不定期副业成功案例分享
*
过于宽松并且会破坏凭据的使用。”?