1.npm 拉取js-cookie 简化cookie操作
npm install js-cookie
2.登录接口import js-cookie 并且开始接受后端传回cookie以及操作给前端浏览器写入cookie
重要代码:
//发起登录请求
LoginApi.login(this.user).then(res=>{
cookie.set("jwt-token",res.data.token,{domain:'localhost'});//写入cookie
if(res.code===20000){
this.$message.success(res.message);
window.location.href="/"
}
else{
this.$message.error(res.message);
}
})
3.重要api需要权限校验的接口使用js-cookie,从域名取出cookie写入header方便后端校验
4.后端取出header调用jwt工具进行认证校验
//获取登录信息
@GetMapping("get-login-info")
public Result getUserInfo(HttpServletRequest request){
JwtInfo jwtInfo = JwtUtil.getMemberInfoByToken(request);
return Result.ok().data("info",jwtInfo);
}