移除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 RedisStore from './lib/redis-store.js'
import MemStore from './lib/mem-store.js' import MemStore from './lib/mem-store.js'
// 会话储存器 // 会话安装包
export const sessionStore = { export const sessionPackage = {
name: 'session', name: 'session',
install() { install() {
var session = this.get('session') 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 opt = this.get('session')
var jwt = this.get('jwt') var cache = req.cookie('NODESSID')
var deviceID = ''
var ssid var ssid
// options请求不处理会话 // options请求不处理会话
@ -35,45 +36,35 @@ export function sessionWare(req, res, next) {
return next() return next()
} }
// jwt模式的校验不在这里处理 // 校验UA
if (jwt) { if (opt.level & 2) {
var auth = req.header('authorization') deviceID += req.header('user-agent')
if (auth) { }
ssid = auth.split('.').pop()
this.$$session.start(ssid) // 校验IP
if (opt.level & 4) {
deviceID += req.ip()
}
if (deviceID) {
deviceID = sha1(deviceID)
// ssid 最后16位是指纹
if (cache) {
if (cache.slice(-16) === deviceID.slice(-16)) {
ssid = cache
} else {
ssid = uuid('') + deviceID.slice(-16)
}
} }
} else { } else {
var cache = req.cookie('NODESSID') ssid = cache || sha1(uuid())
var deviceID = ''
// 校验UA
if (opt.level & 2) {
deviceID += req.header('user-agent')
}
// 校验IP
if (opt.level & 4) {
deviceID += req.ip()
}
if (deviceID) {
deviceID = sha1(deviceID)
// ssid 最后16位是指纹
if (cache) {
if (cache.slice(-16) === deviceID.slice(-16)) {
ssid = cache
} else {
ssid = uuid('') + deviceID.slice(-16)
}
}
} else {
ssid = cache || sha1(uuid())
}
res.cookie('NODESSID', ssid)
this.$$session.start(ssid)
} }
res.cookie('NODESSID', ssid, { maxAge: opt.ttl, domain: opt.domain })
// 缓存ssid到req上
req.ssid = ssid
this.$$session.start(ssid)
next() next()
} }

View File

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

View File

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