Angular-认证头 [英] Angular - authentication headers
本文介绍了Angular-认证头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Java后端,为我提供了一些受身份验证保护的API.
I have an java backend that provides me some API's that are protected with authentication.
因此,如果我使用具有授权功能的任何浏览器或邮递员来调用它,都会收到200 (Ok)
响应,但是如果使用相同的用户名和密码设置httpHeaders
,则会得到401 (Unauthorized)
.
So if I call it using any browser or postman with Authorization I have an 200 (Ok)
response, but If I set httpHeaders
with the same user and password, I get 401 (Unauthorized)
.
通话:
let username: string = 'admin';
let password: string = 'pe';
let headers = new HttpHeaders().set('Authorization' , 'Basic ' + btoa(username + ':' + password)).set('Content-Type', 'application/json').set('cache-control', 'no-cache');
console.log(headers);
let params = new HttpParams().set('instID', value).set('procType', 'M');
return this.httpClient.get<pe_process_instance[]>('http://' + this.urlConfig.BASE_URL + ':' + this.urlConfig.PORT + '/' + this.urlConfig.WSBaseURL + '/getipeprocessinstances', { headers: headers , params: params });
响应:
我在这里想念的是什么?我可以正确设置标题吗?
What I am missing here? Do I correctly set my headers?
推荐答案
尝试以这种方式设置headers
:
let headers = new HttpHeaders();
headers = headers.append('Authorization', 'Basic ' + btoa(username + ':' + password));
headers = headers.append('Content-Type', 'application/json');
headers = headers.append('cache-control', 'no-cache');
多次使用.set()
时,每次都会覆盖标头,并且仅发送最后一个标头.
When you use .set()
multiple times you overwrite your headers each time, and only last header is send.
这篇关于Angular-认证头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文