Angularjs 当路径包含查询字符串时,使用returnUrl重定向问题
我有以下授权拦截器,当发生401时,它会将用户重定向到我的登录路径:Angularjs 当路径包含查询字符串时,使用returnUrl重定向问题,angularjs,Angularjs,我有以下授权拦截器,当发生401时,它会将用户重定向到我的登录路径: app.factory('authHttpResponseInterceptor', ['$q', '$location', function ($q, $location) { return { response: function (response) { if (response.status === 401) { console.log("
app.factory('authHttpResponseInterceptor', ['$q', '$location', function ($q, $location) {
return {
response: function (response) {
if (response.status === 401) {
console.log("Response 401");
}
return response || $q.when(response);
},
responseError: function (rejection) {
if (rejection.status === 401 && $location.path() !== '/') {
$location.path('/login').search('returnUrl', $location.path());
}
return $q.reject(rejection);
}
}
}]);
我希望它将用户试图访问的URL设置为returnURL。不幸的是,如果URL类似于/join?code123&id=456
,它将重定向到/login?code123&id=456&returnUrl=%2Flogin
。如您所见,它将进入拦截器的查询字符串保留在查询字符串中,而不是将其包含在returnUrl
中
然后我想在重定向中使用$location.url()
,如下所示:
$location.path('/login').search('returnUrl', $location.url());
responseError: function (rejection) {
if (rejection.status === 401 && $location.path() !== '/') {
$location.path('/login');
}
return $q.reject(rejection);
}
var login = function (credentials) {
return $http.post('/api/login', credentials).then(function (response) {
var previousUrl = urlParserFilter($rootScope.previousPage);
var path = previousUrl.hash.substr(1);
if (path !== '/login') {
$location.path(path);
}
else {
$location.path('/');
}
});
};
。。。这确实会将完整的查询字符串放入returnUrl
,但查询字符串仍保留在如下路径中:/login?code123&id=456&returnUrl=%2Flogin%3Fcode%3D123%26id%3D456
。好的,要从查询字符串中删除项目,您必须手动清除它们(例如,$location.path().search('code',null)
),这是不对的
提前感谢。我最终以一种稍微不同的方式解决了这个问题 我去掉了returnUrl代码,因此我的拦截器的responseError如下所示:
$location.path('/login').search('returnUrl', $location.url());
responseError: function (rejection) {
if (rejection.status === 401 && $location.path() !== '/') {
$location.path('/login');
}
return $q.reject(rejection);
}
var login = function (credentials) {
return $http.post('/api/login', credentials).then(function (response) {
var previousUrl = urlParserFilter($rootScope.previousPage);
var path = previousUrl.hash.substr(1);
if (path !== '/login') {
$location.path(path);
}
else {
$location.path('/');
}
});
};
我接着补充说:
app.run(['$rootScope', function ($rootScope) {
$rootScope.$on('$locationChangeStart', function (evt, absNewUrl, absOldUrl) {
$rootScope.previousPage = absOldUrl;
});
}]);
然后,我处理登录后重定向的代码如下所示:
$location.path('/login').search('returnUrl', $location.url());
responseError: function (rejection) {
if (rejection.status === 401 && $location.path() !== '/') {
$location.path('/login');
}
return $q.reject(rejection);
}
var login = function (credentials) {
return $http.post('/api/login', credentials).then(function (response) {
var previousUrl = urlParserFilter($rootScope.previousPage);
var path = previousUrl.hash.substr(1);
if (path !== '/login') {
$location.path(path);
}
else {
$location.path('/');
}
});
};
不幸的是,这仍然不能正确地处理查询字符串,但它确实可以处理重定向,我将站点中唯一一个包含查询字符串的区域修改为不同的URL结构。我最后利用图书馆来帮忙