express-session-会话ID和connect.sid之间的区别?

时间:2019-06-23 19:00:40

标签: node.js express sessionid sid

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal value at javax.swing.SpinnerNumberModel.setValue(SpinnerNumberModel.java:443) at javax.swing.JSpinner.setValue(JSpinner.java:354) at timetablemgmt.SpinnerEditor.getTableCellEditorComponent(SpinnerEditor.java:39) at javax.swing.JTable.prepareEditor(JTable.java:5778) at javax.swing.JTable.editCellAt(JTable.java:3512) at javax.swing.plaf.basic.BasicTableUI$Handler.adjustSelection(BasicTableUI.java:1108) at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:1038) at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280) at java.awt.Component.processMouseEvent(Component.java:6530) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2237) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2295) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4523) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467) at java.awt.Container.dispatchEventImpl(Container.java:2281) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) at java.awt.EventQueue$4.run(EventQueue.java:733) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:730) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) session id之间有什么区别?

例如:

connect.sid

结果:

console.log('session id =', req.sessionID)

并且:

session id = CCw2pSpdPf8NRKLQpFH-nlFztEzps24Q 

结果:

console.log('req.headers =', req.headers)

req.headers = { 20:51:34 host: 'localhost:3000', connection: 'keep-alive', 'cache-control': 'max-age=0', 'upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' + '(KHTML, like Gecko) Chrome/73.0.3683.75 ' + 'Safari/537.36', dnt: '1', accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8', cookie: 'connect.sid=s%3ACCw2pSpdPf8NRKLQpFH-nlFztEzps24Q.P04Tx%2FNboFGXvR34HOjpbeh4ogWy58zs%2Bpyde%2FkuUVs', 'if-none-match': 'W/"2f-u+/xADzzu5HL7bySP/YXVKZBlPc"' } CCw2pSpdPf8NRKLQpFH-nlFztEzps24Q

如何在中间件中使用它们来验证用户?

1 个答案:

答案 0 :(得分:2)

会话标识特定的客户端。通常的想法是,会话对象以及您放入会话对象中的任何数据都将保留在服务器上。当用户向您的服务器发出请求时,他们会提供会话cookie,您的会话基础结构将在该会话cookie中查找并​​获取适当的会话对象。然后,您的请求处理程序可以使用该会话对象以及您放入其中的数据以实现所需的任何目的。

会话对象中的数据存储在您的服务器本地,因此是安全的,不会被客户端弄乱。

  

如何在中间件中使用它们来验证用户?

对于身份验证,通常会在会话对象中创建某种状态,以表示用户是否已正确身份验证。如果不是,则要求他们提供凭据。如果是这样,则允许请求继续进行。

这是中间件的一些伪代码。

app.get("/login", (req, res) => {
   // handle login page
   res.sendFile("login.html");
});

app.post("/login", (req, res) => {
   // check auth credentials from the login form
   if (credentials good) {
       req.session.authenticated = true;
       res.redirect("/someOtherPage.html");
   } else {
       req.session.authenticated = false;
       res.redirect("/login.html");
   }

});

// middleware to allow access of already authenticated
app.use((req, res, next) => {
   // check if session already authenticated
   if (req.session.authenticated) {
       next();
   } else {
       res.redirect("/login.html");
   }
});

// route that relies on previous middleware to prove authentication
app.get("/somethingElse", (req, res) => {
   // do something for this authenticated route
});
  

会话ID和connect.sid有什么区别?

cookie具有名称和值。默认情况下,快速会话的cookie名称为connect.sid。 Cookie的值是一个加密密钥,express-session将其用作会话存储的索引。

会话ID是每个会话对象的内部唯一ID。它在会话存储的内部实现中使用。您真的不需要担心这两个是什么。它们在内部用于各种家政用途。

因此connect.sid包含已发送到客户端的cookie值,并且客户端将其显示回服务器。它故意被加密所掩盖,难以伪造或猜测,因此客户端无法猜测会话值。会话ID仅在服务器上使用,并且确实需要这些类型的保护。