gpt4 book ai didi

javascript - 设置 sameSite ="none"和安全 ="true"未修复 Chrome 引发的新 CORS 错误

转载 作者:行者123 更新时间:2023-12-04 03:56:35 24 4
gpt4 key购买 nike

我一直在尝试通过 express-session 修复 Google Chrome 引发的新 CORS 问题但它似乎没有解决它。具体来说,这个错误:

A cookie associated with a cross-site resource at http://plant-app-test.herokuapp.com/ was set without the `SameSite` attribute. It has been blocked, as Chrome now only delivers cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
我当前的堆栈是 Node.js , Express.js , 和 Passport.js用于身份验证。您可能会看到以下配置:
  • app.js

  • require("dotenv").config();

    const path = require("path");
    const cors = require("cors");
    const logger = require("morgan");
    const helmet = require("helmet");
    const express = require("express");
    const mongoose = require("mongoose");
    const bodyParser = require("body-parser");
    const cookieParser = require("cookie-parser");

    const app_name = require("./package.json").name;
    const debug = require("debug")(
    `${app_name}:${path.basename(__filename).split(".")[0]}`
    );

    const app = express();

    // CORS setup
    app.use(
    cors({
    credentials: true,
    preflightContinue: true,
    optionsSuccessStatus: 200,
    origin: process.env.REACT_APP_CLIENT_POINT,
    allowedHeaders: ["Content-Type", "Authorization"],
    methods: ["GET", "POST", "PUT", "HEAD", "PATCH", "DELETE"],
    })
    );

    // Middleware Setup
    app.use(helmet());
    app.use(logger("dev"));
    app.use(cookieParser());
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));

    require("./configs/db.config");
    require("./configs/session.config")(app);
    require("./configs/passport/passport.config.js")(app);

    // Route setup
    app.use("/", require("./routes/index"));
    app.use("/auth", require("./routes/auth"));
    app.use("/app", require("./routes/application"));

    module.exports = app;

  • session-config.js

  • const session = require("express-session");
    const mongoose = require("mongoose");
    const MongoStore = require("connect-mongo")(session);
    const cookieParser = require("cookie-parser");

    module.exports = (app) => {
    app.use(
    session({
    secret: process.env.SESS_SECRET,
    name: "sessionID",
    cookie: {
    sameSite: "none",
    secure: true,
    },
    resave: false,
    saveUninitialized: false,
    store: new MongoStore({
    mongooseConnection: mongoose.connection,
    ttl: 60 * 60 * 24 * 1000,
    }),
    })
    );
    };

  • 护照-config.js

  • const passport = require("passport");

    require("./serializers.config");
    require("./local-strategy.config");
    require("./google-strategy.config");
    require("./facebook-strategy.config");

    module.exports = (app) => {
    app.use(passport.initialize());
    app.use(passport.session());
    };

  • 序列化程序-config.js

  • const passport = require("passport");
    const User = require("../../models/User.model");

    passport.serializeUser(function (user, done) {
    done(null, user._id);
    });

    passport.deserializeUser(async function (id, done) {
    await User.findById(id, function (err, user) {
    if (!err) done(null, user);
    else done(err, null);
    });
    });

    我在 session cookie 上尝试了几种不同的配置,它在我的开发人员上运行良好。环境(都位于 HTTP://localhost 但具有不同的端口)。有人可以帮忙,因为这不允许我为用户保留 session 并利用他们的 id存储在 cookie 中用于我的工作流程?
    谢谢!

    最佳答案

    对于我的 heroku 应用程序和非常相似的情况,我通过添加 proxy : true, 来修复它。到快速 session 配置。我相信这启用了 secure: true 所需的 SSL 通信。 .

    关于javascript - 设置 sameSite ="none"和安全 ="true"未修复 Chrome 引发的新 CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63783949/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com