【问题标题】:nodeJS Session : how to set the attribute "sameSite" to "none" only for some routesnodeJS Session:如何仅针对某些路由将属性“sameSite”设置为“none”
【发布时间】:2022-01-03 02:31:05
【问题描述】:

我搜索了很长时间以将sameSite属性设置为none,仅针对某些路线没有成功。 (sameSite:'none',安全:true) 我找到了一种使用中间件 express.session 的方法,现在不可用。我阅读了 GitHub 上的文档,但没有找到自定义会话的方法 :(

有人能给我一些信息吗... 提前致谢。

【问题讨论】:

  • 我认为您需要添加更多上下文。你想解决什么问题? 'sameSite' 是一个 cookie 属性。您是否尝试根据特定路由或其他内容修改 express-session cookie?
  • 感谢您的帮助。我读到在不需要时将此属性设置为 none 不是一个好习惯。我正在使用 vue.js 来构建一个应用程序,并且我正在使用特定的 URL 与我的服务器对话。我只需要为路径设置为“none”:req.hostname.startsWidth('app.')
  • 我只需要为路径设置会话 cookie (connect.sid) 的“none”:req.hostname.startsWidth('app.')

标签: node.js express-session samesite


【解决方案1】:

终于找到了:)

const cookieMaxAge = moment().endOf('days').diff(moment());
const sessionWeb = {store: ... cookie: { maxAge: cookieMaxAge }};
const sessionApp = {...sessionWeb, cookie: { sameSite: 'none', secure: true, maxAge: cookieMaxAge }};
app.use((req, res, next) => (session(req.hostname.startsWith('app') ? sessionApp : sessionWeb))(req, res, next))

【讨论】:

  • 这似乎不是一个好的解决方案,因为我添加了这段代码,我有一些错误,比如 MaxListenersExceededWarning:Possible EventEmitter memory leak detected。如果我理解正确,则为每个连接创建一个新的中间件:(
  • 最后:app.use(session(sessionWeb)); app.use(function (req, res, next) { if (req.hostname.startsWith('app')) { req.session.cookie.sameSite = "none" req.session.cookie.secure = true } next( ) })
猜你喜欢
  • 1970-01-01
  • 2020-12-20
  • 2020-09-01
  • 2020-12-26
  • 2018-11-13
  • 2020-10-15
  • 1970-01-01
  • 2019-12-18
  • 2020-05-01
相关资源
最近更新 更多