如何使用CookieSession安全选项?



我已经安装了一个名为cookie-session v2.0的NPM,我想用它来使用HTTPS协议将JWT传输到客户端浏览器。但是,当我想根据https://www.npmjs.com/package/cookie-session的文档将安全选项设置为true时,我收到一个错误,指示以下内容:

Argument of type '{ signed: false; secure: any; }' is not assignable to parameter of type '{ httpOnly?: boolean; keys?: any[]; name?: string; overwrite?: boolean; secret?: string; signed?: boolean; }'.
  Object literal may only specify known properties, but 'secure' does not exist in type '{ httpOnly?: boolean; keys?: any[]; name?: string; overwrite?: boolean; secret?: string; signed?: boolean; }'. Did you mean to write 'secret'?

,如果我将鼠标悬停在cookesession上,我可以清楚地看到没有根据文档分配这样的属性。

(alias) cookieSession(options?: {
    httpOnly?: boolean;
    keys?: any[];
    name?: string;
    overwrite?: boolean;
    secret?: string;
    signed?: boolean;
}): Function
import cookieSession

这是目前为止我在expressApp文件中的代码:

const express = require("express");
const cookieSession = require("cookie-session");
const cors = require("cors");
const { errorHandler } = require("./middleware");
const { NotFoundError } = require("./errors");
const routers = require("./routes");
const expressApp = function async(app) {
  app.set("trust proxy", true);
  app.use(express.json());
  app.use(
    cookieSession({
      signed: false,
    })
  );
  app.use(express.urlencoded({ extended: true, limit: "1mb" }));
  app.use(cors());
  app.use("/api/v1", routers);
  app.all("*", async (req, res) => {
    throw new NotFoundError();
  });
  app.use(errorHandler);
};
module.exports = expressApp;

如何解决此问题,以便将安全选项设置为true?

非常感谢你提前!

根据您所链接的文档

Cookie选项-其他选项传递给cookies.get()cookies.set(),允许您在其他设置中控制安全性,域,路径和签名。

当你在app.use中创建cookieSession时,你不能设置cookie是安全的,但你可以在你的路由中设置或获取cookie时设置它。

cookies.set({ secure: true });

查看如何在expressjs中发送安全cookie的答案

相关内容

  • 没有找到相关文章