设置 samesite 前提是必须同时设置Secure属性(Cookie 只能通过 HTTPS 协议发送),否则无效,所以我们先配置 https
先生成ssl 证书
可以查看这里
在本地开发环境配置文件里 config.local.js
npm i egg-cluster --save
config.cluster = {
https: {
key: path.join(__dirname, "../private.pem"), // https 证书绝对目录
cert: path.join(__dirname, "../file.crt"), // https 证书绝对目录
},
};
config.cookies= {
sameSite: 'none',
}
//跨域配置
config.cors = {
origin: "http://www.bh-admin.com", // 本地开发域名
credentials: true,
allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS",
};
在conttroller admin.js
async test() {
const { ctx, app, service } = this;
ctx.cookies.set("user-token", "123456", {
maxAge: 1000 * 60 * 60 * 24 * 7,
httpOnly: false,
encrypt: true, // 是否加密
});
ctx.body = {};
ctx.status = 200;
}
啊。。。舒服了
访问的的时候应该会报错
net::ERR_CERT_AUTHORITY_INVALID
1,点击错误的请求链接,信任即可
2,参考地址这里