Angular7/表示Session不坚持
Angular 7 / Express Session not persisting
我有一个 Angular 7 应用程序连接到 Express API 后端,但 session 似乎没有持续存在。
在 Express 中,我有以下端点:
router.get('/getsession', function(req, res) {
console.log(`Session ID: ${req.session.id}`);
res.status(200).json({ sessionid: req.session.id });
});
这是连续两次运行 /getsession
:
的输出示例
Session ID: NMi8AXhX1wf9xui0WDFwENZ_3QON_iYN
Session ID: pNWcPTeJVlC8rKySw6ty5xSPa9sSME8x
我已经为 Express 启用了凭据 header,所以它会接受它:
const cors = require("cors");
app.use(cors({
credentials: true,
}));
而且我还为 Angular HttpClient
启用了 withCredentials
,这样它将连同 POST 请求一起发送 cookie:
API_URL: string = "http://localdev.com:4200/api";
options = {
headers: new HttpHeaders({
'Content-Type' : 'application/json',
'Cache-Control': 'no-cache',
'Credentials': 'same-origin'
}),
withCredentials: true,
}
getSessionInfo() {
return this.http.get(`${this.API_URL}/users/getsession`, { withCredentials: true })
.pipe(
catchError(this.handleError)
)
}
从 localhost:4200
到 localhost:4044
有一个 Angular 代理,因此可以处理 API 请求。
任何帮助将不胜感激,提前致谢:)
编辑:有趣的是,cookie 被正确地传递到 Express,但是它仍然为每个请求创建一个新的 session。下面是调用/getsession
端点时req.session
的结果。
{ 'if-none-match': 'W/"b8-afvqPuftgTLN3Wn5o/ZQx8jUsv0"',
cookie: '_ga=GA1.2.1851469997.1544357368; _gid=GA1.2.1246771476.1544357368; _gat_gtag_UA_99682244_1=1',
'accept-language': 'en-US,en;q=0.9,bg;q=0.8,mt;q=0.7',
'accept-encoding': 'gzip, deflate',
referer: 'http://localdev.com:4200/user/register',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
accept: 'application/json, text/plain, /',
connection: 'close',
host: 'localdev.com:4044' }
Session ID: XWKGlJPrzYeRBU3Hi7RIAaWpowGU6Fuz
{ 'if-none-match': 'W/"b8-mMGAHD1Tmbv1r5T+YChLkQoq988"',
cookie: '_ga=GA1.2.1851469997.1544357368; _gid=GA1.2.1246771476.1544357368; _gat_gtag_UA_99682244_1=1',
'accept-language': 'en-US,en;q=0.9,bg;q=0.8,mt;q=0.7',
'accept-encoding': 'gzip, deflate',
referer: 'http://localdev.com:4200/user/register',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
accept: 'application/json, text/plain, /',
connection: 'close',
host: 'localdev.com:4044' }
Session ID: T4SnSqGfo9lOWGpiyPQS0LLJgXsRnZ4T
想通了。当 运行 在没有 SSL 证书的开发环境中时,使用上述配置可以正确发送 cookie,但是您还需要将 cookie secure 设置为 false
以便使用它。
是按以下方式做的:
let sessionMiddleware = session({
secret: 'mysecret',
saveUninitialized: true,
resave: true,
cookie: { secure: false },
store: new MemcachedStore({
hosts: ['127.0.0.1:11211'],
})
});
我有一个 Angular 7 应用程序连接到 Express API 后端,但 session 似乎没有持续存在。
在 Express 中,我有以下端点:
router.get('/getsession', function(req, res) {
console.log(`Session ID: ${req.session.id}`);
res.status(200).json({ sessionid: req.session.id });
});
这是连续两次运行 /getsession
:
Session ID: NMi8AXhX1wf9xui0WDFwENZ_3QON_iYN
Session ID: pNWcPTeJVlC8rKySw6ty5xSPa9sSME8x
我已经为 Express 启用了凭据 header,所以它会接受它:
const cors = require("cors");
app.use(cors({
credentials: true,
}));
而且我还为 Angular HttpClient
启用了 withCredentials
,这样它将连同 POST 请求一起发送 cookie:
API_URL: string = "http://localdev.com:4200/api";
options = {
headers: new HttpHeaders({
'Content-Type' : 'application/json',
'Cache-Control': 'no-cache',
'Credentials': 'same-origin'
}),
withCredentials: true,
}
getSessionInfo() {
return this.http.get(`${this.API_URL}/users/getsession`, { withCredentials: true })
.pipe(
catchError(this.handleError)
)
}
从 localhost:4200
到 localhost:4044
有一个 Angular 代理,因此可以处理 API 请求。
任何帮助将不胜感激,提前致谢:)
编辑:有趣的是,cookie 被正确地传递到 Express,但是它仍然为每个请求创建一个新的 session。下面是调用/getsession
端点时req.session
的结果。
{ 'if-none-match': 'W/"b8-afvqPuftgTLN3Wn5o/ZQx8jUsv0"',
cookie: '_ga=GA1.2.1851469997.1544357368; _gid=GA1.2.1246771476.1544357368; _gat_gtag_UA_99682244_1=1',
'accept-language': 'en-US,en;q=0.9,bg;q=0.8,mt;q=0.7',
'accept-encoding': 'gzip, deflate',
referer: 'http://localdev.com:4200/user/register',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
accept: 'application/json, text/plain, /',
connection: 'close',
host: 'localdev.com:4044' }
Session ID: XWKGlJPrzYeRBU3Hi7RIAaWpowGU6Fuz
{ 'if-none-match': 'W/"b8-mMGAHD1Tmbv1r5T+YChLkQoq988"',
cookie: '_ga=GA1.2.1851469997.1544357368; _gid=GA1.2.1246771476.1544357368; _gat_gtag_UA_99682244_1=1',
'accept-language': 'en-US,en;q=0.9,bg;q=0.8,mt;q=0.7',
'accept-encoding': 'gzip, deflate',
referer: 'http://localdev.com:4200/user/register',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
accept: 'application/json, text/plain, /',
connection: 'close',
host: 'localdev.com:4044' }
Session ID: T4SnSqGfo9lOWGpiyPQS0LLJgXsRnZ4T
想通了。当 运行 在没有 SSL 证书的开发环境中时,使用上述配置可以正确发送 cookie,但是您还需要将 cookie secure 设置为 false
以便使用它。
是按以下方式做的:
let sessionMiddleware = session({
secret: 'mysecret',
saveUninitialized: true,
resave: true,
cookie: { secure: false },
store: new MemcachedStore({
hosts: ['127.0.0.1:11211'],
})
});