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
There is no need to say much about the difference...
Website, (100-1)% of the content is navigation 1....
Table of contents Implementation ideas There are ...
This article shares with you the MySQL 8.0.13 ins...
What I have been learning recently involves knowl...
1. Pull the official 5.0.3 image [root@localhost ...
A design soldier asked: "Can I just do pure ...
effect: The GROUP_CONCAT function can concatenate...
Table of contents Start and stop Database related...
location / { index index.jsp; proxy_next_upstream...
Firewall A firewall is a set of rules. When a pac...
This script can satisfy the operations of startin...
<div id="root"> <h2>Keep go...
1. Online installation Currently only tried the L...
As a front-end developer, I can’t avoid IE’s pitf...