【问题标题】:session not persisting in development (localhost:4200)会话未持续开发(本地主机:4200)
【发布时间】:2019-10-10 04:13:36
【问题描述】:

我正在使用 angular(前端)和 node.js + express(后端)。当我在localhost:3000(运行快速应用程序的端口)上运行我的应用程序时,一切都是正确的,我注意到即使我刷新页面,请求会话 ID 也是相同的。但是,当我想在 localhost:4200 上以开发模式工作时,会话不再持久,每次刷新页面时都会显示一个新的会话 ID。

app.js

const bodyParser = require('body-parser'),
  cookieParser = require('cookie-parser'),
  cors = require('cors'),
  express = require('express'),
  session = require('express-session');

const app = express();

app.use(cors());


const port = process.env.PORT || '3000';
app.set('port', port);

const server = http.createServer(app);

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
  next();
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: false
}));

app.use(cookieParser());

app.use(session({
  secret: 'MY-KEY',
  resave: false,
  saveUninitialized: false,
  store: new MongoStore({ mongooseConnection: mongoose.connection })
}));

app.use(passport.initialize());
app.use(passport.session());

server.listen(port, () => console.log(`API running on localhost:${port}`));

我也在开发模式下使用代理,如下所示:

proxy-conf.json

{
  "/api/*": {
    "target": "http://localhost:3000",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true
  }
}

【问题讨论】:

  • 会话配置中没有代理密钥 OLN 可能是罪魁祸首,除非您在其他地方有明确的信任代理设置。 github.com/expressjs/session#proxy 和这里:stackoverflow.com/questions/30802322/…
  • 不确定,您所说的代理是针对 node.js 的,而我帖子中的代理是针对 Angular 应用的
  • 啊,我明白了。在这种情况下,请尝试以下两个选项之一:1)注释掉 Allow-Methods 标头或 2)将“HEAD”添加到允许的方法列表中。

标签: javascript node.js express express-session


【解决方案1】:

找到它,我必须:将localhost:4200 添加到白名单,在session 中将cookie.secure 设置为false

app.js

var whitelist = ['http://localhost:4200'];

var corsOptions = {
  origin: function(origin, callback) {
    if (whitelist.indexOf(origin) === -1) {
      callback(new Error('Not allowed by CORS'));
    } else {
      callback(null, true);
    }
  },
  credentials: true,
};

app.use(cors(corsOptions));
.
.
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
  next();
});
.
.
app.use(session({
  secret: 'My-Key',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: false },
  store: new MongoStore({ mongooseConnection: mongoose.connection })
}));

并在每个 Http 请求头中添加:

withCredentials: true

作为

isLoggedIn(): Observable < boolean > {
    return this.http.get(ip + 'api/isAuthenticated', { withCredentials: true }).map(response => response.json());
}

【讨论】:

    猜你喜欢
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2017-05-16
    • 2011-05-30
    相关资源
    最近更新 更多