React JS节点JS ExpressJS跨站点会话/cookie管理问题
嗨,我试图使用Expressession问题实现身份验证,是我的前端在另一个域上,而我的后端在另一个领域。 假设前端在domaina domaina.com上 现在,当我成功创建了使用Localhost会话的本地计算机并保存cookie时,但是当我部署它并尝试使用domainb.com在domaina.com上创建会话时,则无法设置cookie,因为哪个会话根本不起作用。 我尝试了app.set(代理,1)等,以及互联网上的所有内容。我一直在尝试从几个月来解决这个问题,但我尝试了所有在线解决方案,但它并未从domainb.com上存储cookie。 另外,当我在localhost上映射Anydomain.com时,请尝试登录并将cookie从Localhost:4000到Anydomain.com(映射到Localhost:3000),然后没有保存cookie,则表示Cookie不会保存在跨站点上。
//Server.js file
const dotenv = require('dotenv');
dotenv.config({ path: './config.env' });
const express = require('express');
const cookieParser = require('cookie-parser');
const serverInfo = require('./Info/ServerInfo');
const session = require('./Session/Session');
const cors = require('./Cors/Cors');
const routes = require('./Routes/Routes');
const app = express();
const Server = async () => {
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(session);
app.use(cors);
app.use(routes);
app.listen(serverInfo.port, () => {
console.log(`Server Is Running At Port ${serverInfo.port}`);
});
}
module.exports.start = Server;
//session.js
const session = require('express-session');
const uuid = require('uuid');
const serverInfo = require('../Info/ServerInfo');
module.exports = session({
secret: serverInfo.sessionKey,
resave: false,
saveUninitialized: false,
cookie: {
sameSite: 'none',
httpOnly: true,
secure: false
},
genid: (req) => uuid.v4()
});
Hi i am trying to implement authentication using express-session problem is that my frontend is on another domain and my backend is on another domain.
Suppose frontend is on domain domainA.com while backend is on domain domainB.com
Now when i try on my local computer with localhost session is successfully created and cookie is saved but when i deployed it and try to create session on domainA.com using domainB.com then cookie is not set due to which session is not working at all.
I tried app.set(proxy,1) etc and everything on the internet. I have been trying to solve this problem from months and i tried every solution online but it doesn't store cookie on domainA.com from domainB.com.
Also when i map anydomain.com on localhost then try to login and save cookie from localhost:4000 to anydomain.com (that is mapped to localhost:3000) then cookie is not saved it means cookie is not saved on cross-site.
//Server.js file
const dotenv = require('dotenv');
dotenv.config({ path: './config.env' });
const express = require('express');
const cookieParser = require('cookie-parser');
const serverInfo = require('./Info/ServerInfo');
const session = require('./Session/Session');
const cors = require('./Cors/Cors');
const routes = require('./Routes/Routes');
const app = express();
const Server = async () => {
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(session);
app.use(cors);
app.use(routes);
app.listen(serverInfo.port, () => {
console.log(`Server Is Running At Port ${serverInfo.port}`);
});
}
module.exports.start = Server;
//Session.js
const session = require('express-session');
const uuid = require('uuid');
const serverInfo = require('../Info/ServerInfo');
module.exports = session({
secret: serverInfo.sessionKey,
resave: false,
saveUninitialized: false,
cookie: {
sameSite: 'none',
httpOnly: true,
secure: false
},
genid: (req) => uuid.v4()
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论