如何开发 angularjs 拦截器来控制会话
how to develop angularjs interceptor to control session
我是一名学生,最近在研究 angularJS 拦截器并尝试开发一个来控制会话管理。我是平均堆栈开发的新手,需要帮助。
有人有 angularJS 会话管理的工作示例吗?
非常感谢您的帮助和时间。
如果你想要一个基于令牌的控件,你可以这样做:
你的拦截器:
angular.module('yourApp').factory('YourHttpInterceptor', ['$q', '$window',
function($q, $window) {
return {
'request': function(config) {
config.headers = config.headers || {};
// If you have a token in local storage for example:
if ($window.localStorage.token) {
// Add the token to "Authorization" header in every request
config.headers.Authorization = 'Bearer ' + $window.localStorage.token;
// In your server you can check if the token is valid and if it's not,
// in responseError method you can take some action
}
// Handle something else
return config;
},
// Optional method
'requestError': function(rejection) {
// do something on request error
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
},
// Optional method
'response': function(response) {
// do something on response success
return response;
},
// optional method
'responseError': function(rejection) {
// Here you can do something in response error, like handle errors, present error messages etc.
if(rejection.status === 401) { // Unauthorized
// do something
}
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
}
};
}]);
然后在您的模块配置中注册拦截器:
angular.module('yourApp', []).config(function($httpProvider) {
$httpProvider.interceptors.push('YourHttpInterceptor');
}
正如您在 this post 中看到的,基于令牌的身份验证遵循以下步骤(几乎总是):
- 客户端将其凭据(用户名和密码)发送到服务器。
- 服务器对它们进行身份验证并生成一个带有到期日期的令牌。
- 服务器将之前生成的token存储在一些带有用户标识符的存储中,例如内存中的数据库或地图。
- 服务器将生成的令牌发送给客户端。
- 在每次请求中,客户端都会向服务器发送令牌。
- 服务器在每个请求中,从传入的请求中提取令牌,使用令牌查找用户标识符以获取用户信息以执行authentication/authorization。
- 如果令牌过期,服务器会生成另一个令牌并将其发送回客户端。
我是一名学生,最近在研究 angularJS 拦截器并尝试开发一个来控制会话管理。我是平均堆栈开发的新手,需要帮助。 有人有 angularJS 会话管理的工作示例吗?
非常感谢您的帮助和时间。
如果你想要一个基于令牌的控件,你可以这样做:
你的拦截器:
angular.module('yourApp').factory('YourHttpInterceptor', ['$q', '$window',
function($q, $window) {
return {
'request': function(config) {
config.headers = config.headers || {};
// If you have a token in local storage for example:
if ($window.localStorage.token) {
// Add the token to "Authorization" header in every request
config.headers.Authorization = 'Bearer ' + $window.localStorage.token;
// In your server you can check if the token is valid and if it's not,
// in responseError method you can take some action
}
// Handle something else
return config;
},
// Optional method
'requestError': function(rejection) {
// do something on request error
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
},
// Optional method
'response': function(response) {
// do something on response success
return response;
},
// optional method
'responseError': function(rejection) {
// Here you can do something in response error, like handle errors, present error messages etc.
if(rejection.status === 401) { // Unauthorized
// do something
}
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
}
};
}]);
然后在您的模块配置中注册拦截器:
angular.module('yourApp', []).config(function($httpProvider) {
$httpProvider.interceptors.push('YourHttpInterceptor');
}
正如您在 this post 中看到的,基于令牌的身份验证遵循以下步骤(几乎总是):
- 客户端将其凭据(用户名和密码)发送到服务器。
- 服务器对它们进行身份验证并生成一个带有到期日期的令牌。
- 服务器将之前生成的token存储在一些带有用户标识符的存储中,例如内存中的数据库或地图。
- 服务器将生成的令牌发送给客户端。
- 在每次请求中,客户端都会向服务器发送令牌。
- 服务器在每个请求中,从传入的请求中提取令牌,使用令牌查找用户标识符以获取用户信息以执行authentication/authorization。
- 如果令牌过期,服务器会生成另一个令牌并将其发送回客户端。