我目前正在构建一个webapp,使用后台的Django + Django Rest框架(DRF)和Angularjs构建UI。
所有请求都使用构建到DRF中的基本令牌身份验证进行身份验证。我在request中实现了一个基本的拦截器,将令牌(登录后存储在会话存储中)添加到每个请求中。这在Google (我可以看到授权头实际上是使用检查器添加到每个请求中)和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上。我已经验证了令牌在sessionStorage中的Safari中是可用的。我已经从拦截器将令牌打印到控制台。我无法理解这件事。
有没有人?
发布于 2014-04-08 05:25:37
最后偶然发现了这个恼人的问题的解决办法。实际上,这与Angularjs如何删除url中的尾斜杠以及Django如何使用这些斜线来执行其魔法有关。
由于角中的尾斜杠被移除,服务器响应为301 (永久移动)响应。Django Rest Framework然后将请求重定向到正确的url (包括尾斜杠)。但是,由于某些原因,在IE和Safari中,身份验证令牌没有传递到第二个请求中。这就是你的问题。
希望这有什么帮助。
https://stackoverflow.com/questions/22696560
复制