简体   繁体   English

CORS 403错误Angular和Express

[英]CORS 403 error Angular and Express

Part of my Express configuration looks like this and runs on different domain 我的Express配置的一部分看起来像这样,并且在其他域上运行

app.use(function(req, res, next) {
    res.setHeader("Access-Control-Allow-Origin", 'http://localhost:3000');
    res.setHeader("Access-Control-Allow-Credentials","true");
    res.setHeader("Access-Control-Expose-Headers", "Set-Cookie");
    res.setHeader("Access-Control-Allow-Headers", "Content-Type, x-xsrf-token, X-Requested-With, Accept, Expires, Last-Modified, Cache-Control");
    res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, DELETE");
    next();
});
app.configure('development', 'production', function() {
    app.use(express.csrf());
    app.use(function(req, res, next) {
        res.cookie('XSRF-TOKEN', req.csrfToken());
        next();
    });
});

When using CORS there is OPTIONS request before anything different than a GET. 当使用CORS时,除了GET之外,还有其他OPTIONS请求。 The server is manually configured to respond to it with 200 every time, so it proceed to the POST or whatever. 手动将服务器配置为每次响应200,因此它将继续进行POST或其他操作。 The requests come from Angular. 请求来自Angular。 Some said I need to add some of these lines in order to configure Angular properly. 有人说我需要添加一些这样的行才能正确配置Angular。

$httpProvider.defaults.xsrfCookieName = 'XSRF-TOKEN';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
$httpProvider.defaults.useXDomain = true;
$httpProvider.defaults.withCredentials = true;

The error which I get is 403 Forbidden, the only solution so far is to comment out the whole app.configure(..) in the server. 我得到的错误是403 Forbidden,到目前为止,唯一的解决方案是注释掉服务器中的整个app.configure(..)。 Obviously, I have some problem with CSRF and I cannot understand what should I do. 显然,我对CSRF有一些问题,我不知道该怎么办。

EDITED 已编辑

You need to enable CORS support your angular.js app: 您需要启用CORS支持angular.js应用程序:

yourApp.config(['$httpProvider', function($httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
    }
]);

This should sort you out. 这应该可以解决您的问题。

var allowCrossDomain = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');

    next();
}

app.use(allowCrossDomain);

This section of the doc says that 该文档的这一部分

For requests without credentials, the server may specify "*" as a wildcard, thereby allowing any origin to access the resource. 对于没有凭据的请求,服务器可以将“ *”指定为通配符,从而允许任何源访问资源。

Wildcard is used for completely public APIs which don't require an auth. 通配符用于不需要验证的完全公共的API。 If you want to use credentials (via cookies) you've got to set the exact list of allowed urls in Access-Control-Allow-Origin 如果要使用凭据(通过Cookie),则必须在Access-Control-Allow-Origin设置允许的网址的确切列表

Make sure res.setHeader("Access-Control-Allow-Credentials","true"); 确保res.setHeader("Access-Control-Allow-Credentials","true"); is used at the back end like: 用于后端,例如:

 app.use(function(req, res, next) {
    res.setHeader("Access-Control-Allow-Origin", 'YOUR URL HERE');
    res.setHeader("Access-Control-Allow-Credentials","true");
    res.setHeader("Access-Control-Expose-Headers", "Set-Cookie");
    res.setHeader("Access-Control-Allow-Headers", "Content-Type, x-xsrf-token, X-Requested-With, Accept, Expires, Last-Modified, Cache-Control");
    res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
});

Also the $httpProvider.defaults.withCredentials = true; 还有$httpProvider.defaults.withCredentials = true; should be used at the angular side to allow sending the credentials. 应该在有角度的一侧使用以允许发送凭据。

angular.module('CoolModule')
  .config(['$httpProvider', function($httpProvider){
    // For Access-Control-Allow-Origin and Set-Cookie header
    $httpProvider.defaults.withCredentials = true;
  }]); 

or $http({withCredentials: true, ...}).get(...) $http({withCredentials: true, ...}).get(...)

Hope this helps. 希望这可以帮助。

EDIT: Not a solution, but as a workaround to allow the OPTIONS request this piece of code could be added: 编辑:不是一种解决方案,但作为一种允许OPTIONS请求的解决方法,可以添加以下代码:

...
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
if ('OPTIONS' == req.method) { 
  res.send(200); } else { next(); 
}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM