interceptor 联合 springboot session 和 token 等, 来验证用户是否登录
// 让 axios 反对跨域拜访和 cookie
axios.defaults.withCredentials = true;
axios.interceptors.request.use(
config => {// const token = getCookie('名称'); 留神应用的时候须要引入 cookie 办法,举荐 js-cookie
config.data = JSON.stringify(config.data);
config.headers = {'Content-Type':'application/json;charset=UTF-8'}
return config;
},
error => {return Promise.reject(error);
}
);
//http response 拦截器
axios.interceptors.response.use(
response => {return response;},
error => {return Promise.reject(error)
}
)
后端
// 用户名和明码正确分支 n
if(userList!=null && userList.size()>0){
//
request.getSession().setAttribute("user", userList.get(0));
request.getSession().setAttribute("userid", userList.get(0).getUserid());
return "success";
}else{
// 用户名或则明码谬误分支
return "error";
}
还能够联合 window.sessionStorage 一起应用