使用req.session.destroy()
127.0.0.1:6379> keys *
1) "sess:wKZbEQ7o-DrbxyAkMaKMcA42Dr7nlD55"
127.0.0.1:6379> get sess:wKZbEQ7o-DrbxyAkMaKMcA42Dr7nlD55
"{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2020-09-10T14:24:36.296Z\",\"httpOnly\":true,\"path\":\"/\"},\"username\":\"arden\",\"realname\":\"\xe7\x87\x81\xe7\x87\x81\"}"
127.0.0.1:6379> keys *
//登出后
//存在redis裡面的原本key被刪掉了,又建立一個一個新的key
1) "sess:0t6krtPISdTZE5LIxsJD7pPIjuqmML-a"
127.0.0.1:6379> get sess:0t6krtPISdTZE5LIxsJD7pPIjuqmML-a
"{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2020-09-10T14:25:18.506Z\",\"httpOnly\":true,\"path\":\"/\"}}"
127.0.0.1:6379>
想问删除session数据?是使用res.clearCookie("connect.sid")吗?
127.0.0.1:6379> get sess:O36CjQ8QWwB9lP1eOyEIzUfjRzHdkL02
"{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2020-09-10T14:34:12.410Z\",\"httpOnly\":true,\"path\":\"/\"}}"
//登入后
127.0.0.1:6379> get sess:O36CjQ8QWwB9lP1eOyEIzUfjRzHdkL02
"{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2020-09-10T14:35:52.981Z\",\"httpOnly\":true,\"path\":\"/\"},\"username\":\"arden\",\"realname\":\"\xe7\x87\x81\xe7\x87\x81\"}"
//登出后,原本的key還在,又新增一個key
127.0.0.1:6379> keys *
1) "sess:NzPMO53A83lgXVHtN5BBmr4jeXXLefWK"
2) "sess:O36CjQ8QWwB9lP1eOyEIzUfjRzHdkL02"
127.0.0.1:6379> get sess:O36CjQ8QWwB9lP1eOyEIzUfjRzHdkL02
"{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2020-09-10T14:35:52.981Z\",\"httpOnly\":true,\"path\":\"/\"},\"username\":\"arden\",\"realname\":\"\xe7\x87\x81\xe7\x87\x81\"}"
127.0.0.1:6379> get sess:NzPMO53A83lgXVHtN5BBmr4jeXXLefWK
"{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2020-09-10T14:36:22.391Z\",\"httpOnly\":true,\"path\":\"/\"}}"
127.0.0.1:6379>
虽然旧的key不会被删掉,但采用redis过期删除旧的key,请问是这样的做法吗?
查到资料上是写res.clearCookie()只会删除cookie,并且如果您能以某种方式还原cookie(如果有备份或有人偷了它),则将重新登录(或cookie的任何功能)。
使用req.session.destroy() ,则会话也会在服务器中无效,因此,即使您获得了cookie,也不会获得该会话。