移除jwt相关的;优化ssid

v1
宇天 2020-09-25 18:32:00 +08:00
parent c9c3fb2f2b
commit 0149357fd2
3 changed files with 51 additions and 63 deletions

View File

@ -9,8 +9,8 @@ import { uuid, sha1 } from 'crypto.js'
import RedisStore from './lib/redis-store.js'
import MemStore from './lib/mem-store.js'
// 会话储存器
export const sessionStore = {
// 会话安装包
export const sessionPackage = {
name: 'session',
install() {
var session = this.get('session')
@ -25,9 +25,10 @@ export const sessionStore = {
}
// 会话中间件
export function sessionWare(req, res, next) {
export function sessionConnect(req, res, next) {
var opt = this.get('session')
var jwt = this.get('jwt')
var cache = req.cookie('NODESSID')
var deviceID = ''
var ssid
// options请求不处理会话
@ -35,17 +36,6 @@ export function sessionWare(req, res, next) {
return next()
}
// jwt模式的校验不在这里处理
if (jwt) {
var auth = req.header('authorization')
if (auth) {
ssid = auth.split('.').pop()
this.$$session.start(ssid)
}
} else {
var cache = req.cookie('NODESSID')
var deviceID = ''
// 校验UA
if (opt.level & 2) {
deviceID += req.header('user-agent')
@ -71,9 +61,10 @@ export function sessionWare(req, res, next) {
ssid = cache || sha1(uuid())
}
res.cookie('NODESSID', ssid)
res.cookie('NODESSID', ssid, { maxAge: opt.ttl, domain: opt.domain })
// 缓存ssid到req上
req.ssid = ssid
this.$$session.start(ssid)
}
next()
}

View File

@ -19,16 +19,14 @@ export default class Session {
this.ttl = opt.ttl
}
start(ssid, oldssid) {
start(ssid) {
var session = this.store[ssid]
this.ssid = ssid
// 内存版会话管理, 没有设计计划任务来清理过期数据
// 需要在初始化时先判断, 过期的自动清除, 没过期的, 直接重新续期
if (session) {
if (Date.now() > session.__expires__) {
this.clear()
this.clear(ssid)
}
} else {
session = this.store[ssid] = {}
@ -39,28 +37,28 @@ export default class Session {
}
// 获取session字段值
get(key) {
return key ? this.store[this.ssid][key] || null : this.store[this.ssid]
get(ssid, key) {
return key ? this.store[ssid][key] || null : this.store[ssid]
}
// 设置session字段值
set(key, val) {
set(ssid, key, val) {
if (typeof key === 'object') {
for (let i in key) {
this.store[this.ssid][i] = key[i]
this.store[ssid][i] = key[i]
}
} else {
this.store[this.ssid][key] = val
this.store[ssid][key] = val
}
}
// 删除单个字段
unset(key) {
delete this.store[this.ssid][key]
unset(ssid, key) {
delete this.store[ssid][key]
}
// 清除个人session
clear() {
this.store[this.ssid] = {}
clear(ssid) {
this.store[ssid] = {}
}
}

View File

@ -17,16 +17,15 @@ export default class Session {
}
start(ssid) {
this.ssid = ssid
// 设置session有效期
this.store.expire(ssid, this.ttl)
}
// 获取session字段值, 需要await指令
get(key) {
get(ssid, key) {
var defer = Promise.defer()
this.store.hgetall(this.ssid, (err, obj) => {
this.store.hgetall(ssid, (err, obj) => {
if (err) {
return defer.reject(err)
}
@ -47,23 +46,23 @@ export default class Session {
}
//设置session字段值
set(key, val) {
set(ssid, key, val) {
if (typeof key === 'object') {
for (let i in key) {
this.store.hset(this.ssid, i, key[i])
this.store.hset(ssid, i, key[i])
}
} else {
this.store.hset(this.ssid, key, val)
this.store.hset(ssid, key, val)
}
}
//删除单个字段
unset(key) {
this.store.hdel(this.ssid, key)
unset(ssid, key) {
this.store.hdel(ssid, key)
}
//清除个人session
clear() {
this.store.del(this.ssid)
clear(ssid) {
this.store.del(ssid)
}
}