From 9a686e50335f10532cd3e95cb39535a5fc6541b2 Mon Sep 17 00:00:00 2001 From: yafu Date: Thu, 7 Mar 2024 12:41:32 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=BC=80=E5=90=AFcookie?= =?UTF-8?q?=E7=AD=BE=E5=90=8D=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/app.js | 5 ++++- lib/config.js | 7 ++++++- lib/cookie.js | 7 ++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/app.js b/lib/app.js index 2d707b7..c60b97e 100644 --- a/lib/app.js +++ b/lib/app.js @@ -1,6 +1,6 @@ const path = require('path'); const Koa = require('koa'); -const {app: cfg_app} = require('./config'); +const {app: cfg_app, cookie: cfg_cookie} = require('./config'); const Logger = require('./logger'); const Response = require('./response'); const router = require('./router'); @@ -54,6 +54,9 @@ class App extends Koa // router this.use(router.routes()).use(router.allowedMethods()); + // cookie + cfg_cookie.keys && (this.keys = cfg_app.app_debug ? ['jj.js'] : cfg_cookie.keys); + // types if(cfg_app.app_debug) { const {isFileSync} = require('./utils/fs'); diff --git a/lib/config.js b/lib/config.js index 1af5bf4..3e94e79 100644 --- a/lib/config.js +++ b/lib/config.js @@ -49,6 +49,11 @@ const cache = { clear_time: undefined // (undefined: 清理一次, 0: 关闭自动清理, >0: 为自动清理周期) } +const cookie = { + keys: [Math.random().toString(36).slice(-8)], // cookie密钥 + signed: true // 默认开启签名 +} + const page = { page_key : 'page', // 默认分页标识 key_origin : 'query', // query 或 params @@ -94,6 +99,6 @@ module.exports = { cache: {...cache, ...base_config.cache}, page: {...page, ...base_config.page}, routes: base_config.routes, - cookie: base_config.cookie, + cookie: {...cookie, ...base_config.cookie}, tpl: {...tpl, ...base_config.tpl} }; \ No newline at end of file diff --git a/lib/cookie.js b/lib/cookie.js index 9bda34b..034d59f 100644 --- a/lib/cookie.js +++ b/lib/cookie.js @@ -30,20 +30,21 @@ class Cookie extends Context if(key === undefined) { return this.all(); } - return this.ctx.cookies.get(key, options); + return this.ctx.cookies.get(key, {signed: cfg_cookie.signed, ...options}); } /** * 删除或清理cookie * @public * @param {*} [key] - 为空则清理所有cookie + * @param {import('cookies').GetOption} [options] * @returns {this} */ - delete(key) { + delete(key, options) { if(key === undefined) { this.clear(); } else { - this.ctx.cookies.set(key, '', {maxAge: 0}); + this.ctx.cookies.set(key, '', {...cfg_cookie, ...options, maxAge: 0}); } return this; }