【发布时间】:2014-05-06 22:54:24
【问题描述】:
我目前正在使用 Django + Django Rest Framework (DRF) 在后面构建一个 webapp,并使用 Angularjs 来构建 UI。
所有请求都使用内置在 DRF 中的基本令牌身份验证进行身份验证。我在 Angular 中实现了一个基本的拦截器,以将登录后存储在会话存储中的令牌添加到每个请求。这在 Google Chrome(我可以看到 Authorization 标头实际上是使用检查器添加到每个请求中)和 Firefox 中运行良好。但是在 Safari 中,标头不会添加到某些请求中。
首先,这是我使用的拦截器:
app.factory('authInterceptor', ['$rootScope', '$q', '$window', function($rootScope, $q, $window){
return {
// This method adds the authentication token to each requests' header
request: function(config){
config.headers = config.headers || {};
// add authentication token in header
try {
var token = $window.sessionStorage.token;
} catch(err){
token = undefined;
}
if(token !== undefined){
config.headers.Authorization = 'Token ' + token;
}
return config;
},
response: function(response){
if(response.status === 401){
// user not authenticated
}
return response || $q.when(response);
}
};
}]);
在 Safari 中让我头疼的具体资源之一是:
app.factory('Coach', ['$resource', '$rootScope',
function($resource, $rootScope){
return $resource(
$rootScope.api_url + 'api/coaches/:coachId/?format=json',
{
coachId: '@id'
}
,
{
getByUserId: {method:'GET', url: $rootScope.api_url + 'api/coaches', isArray:false},
update: {method:'PUT'}
}
);
}]);
如果尝试将 withCredentials 添加到 getByUserId 方法但没有成功。
我的拦截器被正确推送到 $httpService。我已验证该令牌在 Safari 的 sessionStorage 中可用。我已将令牌从拦截器打印到控制台。无法解决这个问题。
有人吗?
【问题讨论】:
-
我想我有完全相同的问题,但使用 java 作为后端:( stackoverflow.com/questions/32018621/…
标签: javascript django angularjs safari django-rest-framework