Some understanding about token

Token is generated on the server side.
If the front end uses the username/password to request authentication from the server, and the server authentication succeeds, the server will return the Token to the front end. The front end can bring Token to prove its legal status every time it requests. If this Token is persistent on the server (such as stored in a database), then it is a permanent identity token.

Insert picture description here
It is usually obtained when we log in, and there must be a token value, otherwise there is no way to log in successfully. And when we request data, we also need the token value. In the request interceptor, write the token value into the header. In this way, every time we obtain data, we also have the token value. And the token value is also necessary when we perform page authentication.


// 请求拦截
server.interceptors.request.use(config => {
    
    
  Toast.loading({
    
    
    duration: 5000, //持续展示toast
    message: "加载中……",
    forbidClick: true
  });
  //   登录之后,再次请求时,会携带token进行身份认证
  let token = window.sessionStorage.getItem("token");
  //   console.log(token)
  if (token) {
    
    
    config.headers.authorization = `Bearer ${
      
      token}`;
  }
  //   设备id
  config.headers.deviceid = `${
      
      deviceid}`,
    // 设备类型-固定H5
    (config.devicetype = "H5");
  return config;
});

// 响应拦截
server.interceptors.response.use(res => {
    
    
  Toast.clear();
  return res;
});

Guess you like

Origin blog.csdn.net/weixin_47295135/article/details/109433575