[英]What is the correct way of sending credentials data using Angular Http Headers?
我正在努力尋找發送證書的最佳方法,因為我發現的所有內容似乎都不安全? 這是發送憑據的正確方法嗎? 是否不應該在授權標頭中使用用戶名和密碼之間的分號來設置此名稱?(用戶名:密碼)然后解析為base64字符串? 我是在嚴格要求處理登錄表單上的提交,而不是稍后使用攔截器等進行授權。
login(username: string, password: string) {
return this.http.post<any>(httpsUrl, { username, password })
.pipe(map(user => {
if (user && user.token) {
localStorage.setItem('currentUser', JSON.stringify(user));
this.currentUserSubject.next(user);
}
return user;
}));
}
與其說是嚴格的安全性,不如說是遵循標准的問題。 您嘗試執行的身份驗證通常稱為基本身份驗證。
有一個概述了如何進行基本身份驗證的標准 ,您是正確的,它確實使用了請求標頭和基於64位編碼的字符串。
但是,如果您的Web服務器已經配置為允許您將憑據作為POST正文的一部分發送,那么它的安全性也不會降低。
它只是不遵循標准,可能會或可能不會對您的特定應用程序產生任何影響。
關於安全性方面,只要您使用SSL \\ HTTPS,那么無論您最終使用哪種方法,數據都將通過網絡加密。
在Angular中使用此機制的示例如下
// You need to import this class
import { HttpHeaders } from '@angular/common/http';
login(username: string, password: string) {
const encodedCredentials = btoa(`${username}:${password}`);
const httpOptions = {
headers: new HttpHeaders({
'Authorization': `Basic ${encodedCredentials}`
})
};
// This could now be get because we don't have a body
return this.http.get<any>(httpsUrl, httpOptions)
.pipe(map(user => {
if (user && user.token) {
localStorage.setItem('currentUser', JSON.stringify(user));
this.currentUserSubject.next(user);
}
return user;
}));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.