gpt4 book ai didi

node.js - Hapijs - 服务器重新启动时 HttpOnly cookie 消失

转载 作者:太空宇宙 更新时间:2023-11-04 02:06:53 24 4
gpt4 key购买 nike

当我将 cookie 定义为 HttpOnly 时,该 cookie 会在服务器重新启动后从 request.state 中消失。这似乎不是 HttpOnly 规范的一部分。 HapiJS 有办法解决这个问题吗?

// cookie definition
server.state('my_cookie', {
ttl: YEAR_IN_MS,
isSecure: false,
isHttpOnly: true,
encoding: 'base64json',
clearInvalid: false,
strictHeader: true,
path: '/'
}

server.ext('onPreResponse', function(request, reply) {
console.log(request.state.my_cookie) // returns undefined after server restart

reply.continue()
})

最佳答案

检查以下代码片段,服务器重启后cookie仍然有效。

const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({ port: 8005, host: 'localhost' });

server.start((err) => {
if (err) {
throw err;
}
console.log(`Server running at: ${server.info.uri}`);
});

server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
//Cookie: { "foo": "Bar0.3077739876826606" }
reply('Cookie: ' + JSON.stringify(request.state.my_cookie, null, '\t'))
}
});

server.route({
method: 'GET',
path: '/set-cookie',
handler: function (request, reply) {
return reply('Success').state('my_cookie', { foo: 'Bar' + Math.random() })
}
});

const YEAR_IN_MS = 86400000 * 365
// cookie definition
server.state('my_cookie', {
ttl: YEAR_IN_MS,
isSecure: false,
isHttpOnly: true,
encoding: 'base64json',
clearInvalid: false,
strictHeader: true,
path: '/'
})

server.ext('onPreResponse', function(request, reply) {
console.log(request.state.my_cookie) //{ foo: 'Bar0.3077739876826606' }
reply.continue()
})

关于node.js - Hapijs - 服务器重新启动时 HttpOnly cookie 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43900239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com