cookies穿透

由于客户端的http请求首先达到SSR服务器,再由SSR服务器去后端服务器获取相应的接口数据。在客户端到SSR服务器的请求中,客户端是携带有cookie数据的。但是在SSR服务器请求后端接口的过程中,却是没有相应的cookie数据的。因此在SSR服务器进行接口请求的时候,我们需要手动拿到客户端的cookie传给后端服务器。

在ssr期间我们需要截取客户端的cookie,保持用户会话唯一性。

1. 在ssr.js中,将cookies注入到context

......
const context = {
    url: ctx.url,
    title: 'VUE-SSR',
    cookie: ctx.cookie //把cookie 注入到 context 中
}
......
renderer.renderToString(context, (err, html) => {
......
}
......

2. 在server-entry.js中,将cookies注入到store

export default context => {
......
const cookie = context.cookie
  if (context) {
    store.state.cookies = context.cookies
  }
......
// 将http放到store工厂里, 防止cookies污染, 解决客户端与服务端cookie穿透
store.$http = store.state.$http = http(cookie) 
......
}
Copyright ©大数据项目组 2019 all right reserved,powered by Gitbook本文档更新于: 2019-11-25 02:41

results matching ""

    No results matching ""