http post - 如何发送Authorization标头? [英] http post - how to send Authorization header?

查看:2157
本文介绍了http post - 如何发送Authorization标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Angular2 RC6中为您的http请求添加标头?
我得到以下代码:

How do you add headers to your http request in Angular2 RC6? I got following code:

login(login: String, password: String): Observable<boolean> {
    console.log(login);
    console.log(password);
    this.cookieService.removeAll();
    let headers = new Headers();
    headers.append("Authorization","Basic YW5ndWxhci13YXJlaG91c2Utc2VydmljZXM6MTIzNDU2");
    this.http.post(AUTHENTICATION_ENDPOINT + "?grant_type=password&scope=trust&username=" + login + "&password=" + password, null, {headers: headers}).subscribe(response => {
      console.log(response);
    });
    //some return
}

问题是,有角度没有'添加授权标头。取而代之的是,在请求中我可以看到以下附加标题:

The problem is, that angular doesn't add Authorization header. Instead of that, in request I can see following additional headers:

Access-Control-Request-Headers:authorization
Access-Control-Request-Method:POST

并在Accept-Encoding中添加sdch:

and sdch added in Accept-Encoding:

Accept-Encoding:gzip, deflate, sdch

未知,没有授权标头。我该如何正确添加?

Unfornately there is no Authorization header. How should I add it correctly?

我的代码发送的整个请求如下:

Whole request sent by my code looks as follow:

OPTIONS /oauth/token?grant_type=password&scope=trust&username=asdf&password=asdf HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: POST
Origin: http://localhost:3002
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Access-Control-Request-Headers: authorization
Accept: */*
Referer: http://localhost:3002/login
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,pl;q=0.6


推荐答案

好的。我发现了问题。

它不在Angular方面。说实话,完全没有问题。

It was not on the Angular side. To be honest, there were no problem at all.

我无法成功执行请求的原因是我的服务器应用程序没有正确处理OPTIONS请求。

Reason why I was unable to perform my request succesfuly was that my server app was not properly handling OPTIONS request.

为什么OPTIONS,而不是POST?我的服务器应用程序位于不同的主机上,然后是前端。由于CORS,我的浏览器将POST转换为OPTION:
http://restlet.com/blog/2015/12/15/understanding-and-using-cors/

Why OPTIONS, not POST? My server app is on different host, then frontend. Because of CORS my browser was converting POST to OPTION: http://restlet.com/blog/2015/12/15/understanding-and-using-cors/

在这个答案的帮助下:
独立Spring OAuth2 JWT授权服务器+ CORS

With help of this answer: Standalone Spring OAuth2 JWT Authorization Server + CORS

我在我的服务器端应用程序上实现了正确的过滤器。

I implemented proper filter on my server-side app.

感谢@Supamiu - 这个人这让我觉得我根本不发邮件。

Thanks to @Supamiu - the person which fingered me that I am not sending POST at all.

这篇关于http post - 如何发送Authorization标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆