1
我有一個角模板。在app.js我有這樣的配置:什麼是認證攔截在角
app.config(function ($routeProvider, $httpProvider) {
$httpProvider.interceptors.push('AuthInterceptor');
$routeProvider
.when('/', { .....etc
的AuthInterceptor是這家工廠:
'use strict';
app.factory('AuthInterceptor', function ($rootScope, $q, $window, $location) {
return {
request: function (config) {
config.headers = config.headers || {};
if ($window.localStorage.token) {
config.headers.Authorization = 'Token ' + $window.localStorage.token;
}
return config;
},
responseError: function (response) {
if (response.status === 401) {
$window.localStorage.removeItem('token');
$window.localStorage.removeItem('email');
$location.path('/');
return;
}
return $q.reject(response);
}
};
});
是什麼究竟做什麼?
添加授權頭包含一個令牌。令牌存儲在localStorage中,並在服務器返回401時刪除。 – JEY