当前位置:网站首页>Save token get token refresh token send header header
Save token get token refresh token send header header
2022-06-29 06:59:00 【51CTO】
// test token
// obtain token
function setToken(data){
var storage = window.localStorage;
if(!storage){
alert(" Browser does not support localstorage");
return false;
}
var token = data.access_token;
storage.setItem("user_token", token);
storage.setItem("user_login_time", new Date().getTime());// Save login time
}
function getToken()
{
var storage = window.localStorage;
var current = new Date().getTime();// take token Time for
var token = storage.user_token;//token Value
var old_time = storage.user_login_time;// Time of login
var time = (current - old_time) /1000;// Time difference
var out_time = 3600;// timeout
var parse_time = 24 * 60 * 60;// Refresh the set
// Clear cache manually
var length = window.localStorage.length;
if(length==0){
window.location.href = "login.html"
}
// Overtime
if(parse_time > time && time >= out_time) {
refreshToken(token);
}
//1 Days overdue Log back in
if(time >= parse_time) {
window.location.href = "login.html";
}
return token;
}
function headerSetup(token)
{
$.ajaxSetup({
'headers':{
'Authorization': 'Bearer '+ token,
}
});
}
// Refresh token
function refreshToken(token)
{
if(window.localStorage.length==0){
window.localStorage.clear();
window.location.href = "login.html";
}
$.ajaxSetup({
'headers': {
'Authorization': 'Bearer ' + token,
}
});
$.ajax({
url:"http://ezist.cn/api/authorizations/current",
type:'post',
dataType: "json",
data: {},
success: function(data) {
setToken(data);
},
error: function (data) {
console.log(" Data request failed ");
window.location.href='login.html';
}
});
}
// test token
// obtain token
function setToken(data){
var storage = window.localStorage;
if(!storage){
alert(" Browser does not support localstorage");
return false;
}
var token = data.access_token;
storage.setItem("user_token", token);
storage.setItem("user_login_time", new Date().getTime());// Save login time
}
function getToken()
{
var storage = window.localStorage;
var current = new Date().getTime();// take token Time for
var token = storage.user_token;//token Value
var old_time = storage.user_login_time;// Time of login
var time = (current - old_time) /1000;// Time difference
var out_time = 3600;// timeout
var parse_time = 24 * 60 * 60;// Refresh the set
// Clear cache manually
var length = window.localStorage.length;
if(length==0){
window.location.href = "login.html"
}
// Overtime
if(parse_time > time && time >= out_time) {
refreshToken(token);
}
//1 Days overdue Log back in
if(time >= parse_time) {
window.location.href = "login.html";
}
return token;
}
function headerSetup(token)
{
$.ajaxSetup({
'headers':{
'Authorization': 'Bearer '+ token,
}
});
}
// Refresh token
function refreshToken(token)
{
if(window.localStorage.length==0){
window.localStorage.clear();
window.location.href = "login.html";
}
$.ajaxSetup({
'headers': {
'Authorization': 'Bearer ' + token,
}
});
$.ajax({
url:"http://ezist.cn/api/authorizations/current",
type:'post',
dataType: "json",
data: {},
success: function(data) {
setToken(data);
},
error: function (data) {
console.log(" Data request failed ");
window.location.href='login.html';
}
});
}
边栏推荐
- VerilogA - counter
- QT program packaging and publishing windeployqt tool
- try anbox (by quqi99)
- Qt 程序打包发布-windeployqt工具
- Overlay histogram with density curve
- centos下配置mysql 5.7 和 8
- [MySQL technology topic] technical analysis and guide for analyzing the high availability architecture of MySQL
- QT (x): control operation
- Labor skills courses integrated into steam Education
- NoSQL数据库之Redis(五):Redis_Jedis_测试
猜你喜欢

Configuring the flutter development environment

关于 localStorage 的一些高阶用法

Easy to understand TCP four waves (multi picture explanation)

Draw multiple ROC curves on a graph

QT qlineedit details

Chapter V online logic analyzer signaltap

分享 10 个 JS Promise 相关的面试题

How to change the password after forgetting the MySQL password (the latest version of 2022 detailed tutorial nanny level)

Delete tag

Open source 23 things shardingsphere and database mesh have to say
随机推荐
开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事
Li Kou daily question - day 30 -594 Longest harmonic subsequence
Overlay histogram with density curve
把多个ROC曲线画在一张图上
Illustrate plug-in -- AI plug-in development -- creative plug-in -- astute graphics -- length and angle measurement function
Character pointer as function parameter
centos下配置mysql 5.7 和 8
QT qframe details
Linux Installation redis
The echares map is implemented separately by provinces, and the tooltip user-defined prompt box, scattered annotation and scattered illumination are explained in detail
Json对象和Json字符串的区别
Small program large screen adaptation Guide
NoSQL数据库之Redis(五):Redis_Jedis_测试
[when OSPF introduces direct connection routes, it makes a summary by using static black hole routes]
Presto-Trial
Qt QFileInfo简介
mongostat性能分析
Open source 23 things shardingsphere and database mesh have to say
Exclusive download. Alibaba cloud native brings 10+ technical experts to bring new possibilities of cloud native and cloud future
NoSQL数据库之Redis(四):Redis新数据类型