question: There are 2 tokens in the project, one with a validity period of 2 hours (referred to as: short token), and the other with a validity period of 14 days (referred to as: long token). answer: 1. Based on security considerations and to prevent token leakage, all requests in server resources can only use short tokens, and short tokens are only valid for 2 hours;
Only in this interface can you send requests using long Token. 2. To improve the user experience, do not directly ask users to exit the page they are operating import Vue from 'vue' import axios from 'axios' import VueAxios from 'vue-axios' import { getToken, setToken } from './token' import router from '../router/index.js' import { Toast } from 'vant' Vue.use(VueAxios, axios) const instance = axios.create({ baseURL: 'base URL', timeout: 100000 }) // Add request interceptor instance.interceptors.request.use( function (config) { // Add token uniformly getToken() && (config.headers['Authorization']= `Bearer ${getToken().token}`) return config }, function (error) { return Promise.reject(error) } ) //Add response interceptor/** * 1.if 401 else don't care* 2.if there is a token else jump to the login page* 3.try-catch use refresh_token to get the token, if successful else refresh_token is invalid, jump to the login page* 4.Save the obtained token, update, and continue to perform the user's desired operation*/ instance.interceptors.response.use( function (response) { return response }, async function (error) { if (401 === error.response.status) { setTimeout('console.clear()', 2000) if (getToken()) { try { // Logged in, but the short T expired, use the long T to get the short T (refresh user token) let res = await axios({ url: 'base address/v1_0/authorizations', method: 'PUT', headers:{Authorization : `Bearer ${getToken().refresh_token}`} }) // Update short T let token = getToken() token.token = res.data.data.token setToken(token) // Continue user operation return instance(error.config) } catch (error) { // Long T fails, jump to login page Toast.fail('Please log in first') router.push({ path: '/login' }) } } else { // Not logged in, jump to the login page Toast.fail('Please log in first') router.push({ path: '/login' }) } } return Promise.reject(error) } ) export default instance The above is the detailed content of the analysis of the reasons and sample codes of 2 Tokens in web project development. For more information about web project development, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Dockerfile text file usage example analysis
>>: Summary of MySQL database like statement wildcard fuzzy query
1. Problem description The storage installed in t...
When starting MongoDB, the prompt is: error while...
Table of contents Overview What are Generics Buil...
Related knowledge points Passing values from pa...
Setting the font for the entire site has always b...
1. Create a table using HTML tags: Copy code The ...
Table of contents Vite project build optimization...
What is a mata tag The <meta> element provi...
Table of contents Vue custom directive Custom dir...
HTTP Status Codes The status code is composed of ...
“Inputs should be divided into logical groups so ...
Table of contents 1.1Tinyint Type Description 1.2...
We all know that Apache can easily set rewrites f...
React multiple ways to get the value of the input...
Preface: In the daily use of the database, it is ...