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

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

问题描述

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

login(login: String, password: String): Observable{控制台日志(登录);控制台日志(密码);this.cookieService.removeAll();让标题=新标题();headers.append("授权","基本YW5ndWxhci13YXJlaG91c2Utc2VydmljZXM6MTIzNDU2");this.http.post(AUTHENTICATION_ENDPOINT + "?grant_type=password&scope=trust&username=" + login + "&password=" + password, null, {headers: headers}).subscribe(response => {控制台日志(响应);});//一些回报}

问题是,angular 没有添加 Authorization 标头.取而代之的是,在请求中,我可以看到以下附加标头:

Access-Control-Request-Headers:authorization访问控制请求方法:POST

和在 Accept-Encoding 中添加的 sdch:

Accept-Encoding:gzip, deflate, sdch

不幸的是,没有 Authorization 标头.我应该如何正确添加?

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

OPTIONS/oauth/token?grant_type=password&scope=trust&username=asdf&password=asdf HTTP/1.1主机:本地主机:8080连接:保持连接编译指示:无缓存缓存控制:无缓存访问控制请求方法:POST来源:http://localhost:3002用户代理:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36Access-Control-Request-Headers:授权接受: */*引用者:http://localhost:3002/login接受编码:gzip、deflate、sdch接受语言:en-US,en;q=0.8,pl;q=0.6

解决方案

好的.我发现了问题.

它不在 Angular 一侧.老实说,完全没有问题.

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

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

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

我在我的服务器端应用上实现了适当的过滤器.

感谢@Supamiu - 指责我根本不发送 POST 的人.

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

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

解决方案

Ok. I found problem.

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

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

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/

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

I implemented proper filter on my server-side app.

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

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

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