我的Vue.js应用出现问题。我有一个Express后端及其上的授权,并且我使用passport.js来实现它。接下来,我有Vue.js应用程序,如果用户未获得授权,则应该打开无法打开的页面,我想检查一下。
我尝试使用Cookie,但是经过一些变型后,我终于对此感到困惑。我的浏览器正在为我的服务器保存cookie,由于“仅HTTP”标志,我无法在客户端上读取它,并且我无法通过get请求检查授权,因为idk(客户端上没有cookie?)。
所以,什么是最好的制作方法?
所以我像这样在Express中添加了标题
const allowCrossDomain = function (req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:8080');
res.header('Access-Control-Allow-Methods', '*');
res.header('Access-Control-Allow-Headers', 'origin, content-type, accept');
res.header('Access-Control-Allow-Credentials', true);
next();
}
app.use(allowCrossDomain);
然后查看授权
axios.get('url',{withCredentials:true})
和登录
axios.post('login',{withCredentials:true})
像那样