简体   繁体   English

如何从客户端访问cookie会话?

[英]How can I access cookie-session from client side?

I am building an application single page using NodeJS, and want to use my cookie session (cookie-session npm) to verify if the user is logged in or not. 我正在使用NodeJS构建应用程序单页,并希望使用我的cookie会话(cookie-session npm)来验证用户是否已登录。 From my node server side I can get and set the session cookie, but I do not know how to get from my client side. 从我的节点服务器端,我可以获取并设置会话cookie,但我不知道如何从我的客户端获取。

This is how I am setting up from my server side: 这就是我从服务器端设置的方式:

req.session.user_id = user[0]._id;

Where user[0]._id is my user id that I get from my mongodb. user[0]._id是我从我的mongodb获取的用户ID。

So let's assume you've configured cookie-session something like this: 所以我们假设你已经配置了像这样的cookie-session

var cookieSession = require('cookie-session');

app.use(cookieSession({
    keys: ['secret']
}));

Then let's store some data in the session: 然后让我们在会话中存储一些数据:

req.session.user_id = 123;

If you look in your browser's dev tools you'll see 2 cookies set: 如果您查看浏览器的开发工具,您将看到2个cookie集:

express:sess = eyJ1c2VyX2lkIjoxMjN9
express:sess.sig = 01I_Rx2gACezZI1tdl2-NvxPq6w

The cookie express:sess is base64 encoded. cookie express:sess是base64编码的。 If we decode it we get {"user_id":123} . 如果我们解码它,我们得到{"user_id":123} It's important to appreciate that the session data is being stored in the cookie itself - this isn't just an id for the session. 重要的是要理解会话数据存储在cookie本身中 - 这不仅仅是会话的ID。

The other cookie, express:sess.sig , is the signature. 另一个cookie, express:sess.sig ,是签名。 This signature is generated using the key ( secret in this example) and is used to help prevent tampering. 此签名使用密钥(在此示例中为secret )生成,用于帮助防止篡改。 It's easy for anyone to modify express:sess but unless they can also generate the corresponding express:sess.sig the server will know it's been changed. 任何人express:sess.sig容易修改express:sess但是除非他们也可以生成相应的express:sess.sig服务器会知道它已被更改。

All that said, I suggest you take a look at the express-session middleware. 总而言之,我建议你看一下express-session中间件。 That also uses cookies but it only uses them to store the session id. 这也使用cookie但它只使用它们来存储会话ID。 No data is stored in the cookie, that is all stored on the server. cookie中没有存储数据,这些数据都存储在服务器上。 This is much more akin to how sessions work in most other web frameworks but I can't say for certain which approach is best suited to your needs. 这更类似于会话在大多数其他Web框架中的工作方式,但我无法确定哪种方法最适合您的需求。

Whichever approach you use the cookie with be set to httponly by default. 无论您使用哪种方法,默认情况下httponly将cookie设置为httponly You'll be able to verify this in your browser's dev tools. 您将能够在浏览器的开发工具中验证这一点。 This means that it's included on HTTP requests but isn't accessible via client-side JavaScript. 这意味着它包含在HTTP请求中,但无法通过客户端JavaScript访问。 This is a security measure designed to make it more difficult for malicious code to steal the cookie. 这是一项安全措施,旨在使恶意代码更难以窃取cookie。 You can disable this security feature in cookie-session using: 您可以使用以下命令在cookie-session禁用此安全功能:

app.use(cookieSession({
    httpOnly: false,
    keys: ['secret']
}));

You'll then be able to access those cookies using document.cookie . 然后,您就可以使用document.cookie访问这些cookie。

I reiterate that this is a security measure and turning it off isn't recommended. 我重申这是一项安全措施,不建议将其关闭。 It's impossible for me to judge whether this is a genuine concern in your application. 我无法判断这是否是您申请中真正关注的问题。

It isn't clear from your question whether you actually want to parse the values out of the cookie or just check for its existence. 从您的问题中不清楚您是否确实想要从cookie中解析值或只是检查它的存在。 If you need to parse it then you'll need to base64 decode the relevant cookie value and then JSON decode it. 如果你需要解析它,那么你需要base64解码相关的cookie值,然后JSON解码它。

There are various alternative approaches you might adopt to keep the cookies httponly . 您可以采用各种替代方法来保持cookie的httponly Without knowing more about what you're going to do with this information it's difficult to be specific. 如果不了解您将如何处理这些信息,则很难具体说明。 If you're using Express views (ie template rendering) then you can do all the work in the template. 如果您正在使用Express视图(即模板渲染),那么您可以在模板中完成所有工作。 If you're in SPA territory then you could maybe use an AJAX request to gather the relevant information. 如果您在SPA区域,那么您可以使用AJAX请求来收集相关信息。 At a pinch you could even use another cookie to give you the information you need while keeping the session cookies safe. 在紧要关头,你甚至可以使用另一个cookie来提供所需的信息,同时保证会话cookie的安全。

Session is a server thing, you cannot access it on client side, if you mean cookie, well, cookie doesn't contain anything about session but an ID pointed to it. Session是一个服务器的东西,你不能在客户端访问它,如果你的意思是cookie,那么,cookie不包含任何有关会话但指向它的ID。 If you want to get info from session on client side, you must create a request, and the server sends the session info back. 如果要从客户端的会话中获取信息,则必须创建请求,然后服务器将会话信息发回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM