实现身份认证,使用Token,JWT
实现的基本思想:
1:在后端使用Jwt生成密钥,在一些页面比如登录页面,注册页面,在这些页面可以关闭身份认证,在前端发起请求后,将生成的密钥返回给客户端。
2:客户端封装了 sessionStorage与localStoreage,客户端借助axiso拦截器与pinia Store实现数据的本地存储与全局状态管理
1封装本地存储
const enum storegeType {
l = 'localStorage',
s = 'sessionStorage'
}
interface TimeLimit {
value:any
time?:number
}
class MyStorege{
storge:Storage
constructor(type:storegeType){
this.storge = type === storegeType.l? window.localStorage:window.sessionStorage;
}
set(key:string,value:any,time_:number|boolean = false){
const timeLimit:TimeLimit = {
value:null
}
if(time_){
timeLimit.time = new Date().getDate() + 1000*60*60*24*365
}
timeLimit.value = value;
const data = JSON.stringify(timeLimit)
this.storge.setItem(key,data)
}
get(key:string){
const item =this.storge.getItem(key)
if(item){
const timeLimit:TimeLimit = JSON.parse(item)
const time = timeLimit.time
if(time){
if(new Date().getTime()> time){
this.storge.removeItem(key)
return null
}
else {
return timeLimit.value
}
}
return timeLimit.value
}
return this.storge.getItem(key)
}
clear(){
this.storge.clear()
}
rem