[英]Ember simple auth and cookie headers
我目前正在使用Ember 2.4.x應用程序,並且正在將ember-simple-auth
與自定義身份驗證器和自定義授權者結合使用。
身份驗證成功后,服務器將以json主體進行響應,該主體包含有關當前用戶的信息以及響應頭中的cookie:
Response Headers:
Set-Cookie:Authorization=1s39gpzqy4d0w1quxekavz6yj1;Path=/
現在,對於每個連續的請求,我唯一需要的就是發回相同的cookie。 我了解使用ember-simple-auth
,可以使用block
回調來在自定義授權者中設置其他標頭。 但是,我找不到一種方法來針對每個請求重新發送完全相同的cookie。
我也想知道我是否根本不需要授權者,因為我沒有設置任何標題,服務器只在乎那個cookie。
如果您的身份驗證服務器發出cookie,那么您根本就不需要授權者,因為瀏覽器會在每次連續請求時自動發送cookie。
我終於設法使它起作用。 基本上,我首先關注該主題的答案
然后,在我的自定義身份驗證器中,我重新使用必須設置xhrFields
屬性的makeRequest
方法:
makeRequest(data, options) {
let serverTokenEndpoint = get(this, 'serverTokenEndpoint');
let requestOptions = $.extend({
url: serverTokenEndpoint,
type: 'POST',
// The contentType must be passed to Jetty as it will default to
// 'application/x-www-form-urlencoded'
contentType: 'application/json',
crossDomain: true,
dataType: 'json',
data,
xhrFields: {
withCredentials: true
},
beforeSend(xhr, settings) {
xhr.setRequestHeader('Accept', settings.accepts.json);
}
}, options || {});
return $.ajax(requestOptions);
}
我還發現服務器必須在Response Headers
設置Access-Control-Allow-Credentials:true
,否則Ember會遇到錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.