我设置了标题和http调用,如下所示:
var headers = new Headers();
headers.set('Authorization','Bearer xxxxxxxxxxx');
this.http.get('http://localhost:8080/outputEndpoints', {
headers: headers
}).
map(res => res.text())
.subscribe(
data=> console.log(data),
error=> console.log("Getting Error")
);有了这个,我希望标题是:
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:es,ca;q=0.8,en-GB;q=0.6,en;q=0.4,fr;q=0.2
Access-Control-Request-Headers:authorization, content-type
Authorization: "Bearer xxxxxxxxxxxx"
Access-Control-Request-Method:GET
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36相反,我得到了以下内容(令牌没有授权):
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:es,ca;q=0.8,en-GB;q=0.6,en;q=0.4,fr;q=0.2
Access-Control-Request-Headers:authorization, content-type
Access-Control-Request-Method:GET
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36编辑:
一般的信息是:
Request URL:http://localhost:8080/outputEndpoints
Request Method:OPTIONS
Status Code:401
Remote Address:[::1]:8080所以期权呼叫失败了。在其他应用程序中,一旦选项调用被接受,它就会发出GET请求,在那里您可以找到设置了身份验证令牌。这意味着,由于某种原因,期权看涨是不被接受的。
当使用CURL或SOAP进行调用时,它工作得很好,所以我假设这不是CORS的问题。我是对的?
任何想法或指南都会很有帮助。
发布于 2017-02-10 12:45:43
按照本指南http://restlet.com/blog/2015/12/15/understanding-and-using-cors/,您可以阅读以下内容:
一个重要的评论。您必须考虑到,在执行包含安全性的CORS请求(即授权头)时,选项请求不会包含它。因此,在处理预置选项的第一个选项请求时,您需要小心安全性。事实上,在这个级别上无法进行身份验证检查。
因此,在第一个选项调用中没有授权头。
发布于 2017-02-08 17:39:55
我想是headers.append而不是headers.set。试着改变你的方法,看看它是否有效。
发布于 2017-02-08 17:43:34
您应该在标头的构造函数中设置如下值:
var headers = new Headers({'Authorization', 'Bearer XXXXXXX'});或者您必须使用附加方法:
var headers = new Headers();
headers.append('Authorization', 'Bearer XXXXXX');因问题更改而编辑:
由于我真的不知道您如何处理您的请求,所以我可以向您展示我在Spring后端中提出的解决方案:
if ("OPTIONS".equals(request.method, ignoreCase = true)) {
response.status = HttpServletResponse.SC_OK
} else {
chain.doFilter(req, res)
}因此,在后端,您应该检查request方法,如果它等于"OPTIONS“,则可以将响应状态设置为200,这样就完成了。
https://stackoverflow.com/questions/42118733
复制相似问题