我在data.domain.com上有一个NodeJS express服务器,而我的AngularJS客户端位于subdomain.domain.com。我正在使用passport / express在服务器上创建会话。然后我的客户端尝试连接到同一服务器上的socket.io.我在socket.io连接上得到403(Forbidden)。
我认为这是一个跨域问题。我在快速服务器上启用了COR。我正在从我的服务器data.domain.com设置一个带有TLD的cookie,即快速cookie域配置是.domain.com。
我已经检查过我的会话cookie是在客户端设置的 - 'expressSid'与TLD .domain.com。当我注释掉io.set的块时,一切都会立即生效(“授权”......
所有内容都在HTTPS下运行。我正在使用RedisStore进行会话存储。
Passport.io / socket.io config:
io.configure(function () {
io.set('transports', ['xhr-polling']);
io.set('polling duration', 10);
io.set('log level', 1);
io.set("authorization", passportSocketIo.authorize({
cookieParser: express.cookieParser, //or connect.cookieParser
key: 'expressSid', //the cookie where express (or connect) stores its session id.
secret: expressSecret, //the session secret to parse the cookie
store: sessionStore, //the session store that express uses
fail: function(data, accept) { // *optional* callbacks on success or fail
accept(null, false); // second param takes boolean on whether or not to allow handshake
},
success: function(data, accept) {
accept(null, true);
}
}));
});
快速配置:
var allowCrossDomain = function(req, res, next) {
var oneof = false;
if(req.headers.origin) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Credentials', true);
oneof = true;
}
if(req.headers['access-control-request-method']) {
res.header('Access-Control-Allow-Methods', req.headers['access-control-request-method']);
oneof = true;
}
if(req.headers['access-control-request-headers']) {
res.header('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
oneof = true;
}
if(oneof) {
res.header('Access-Control-Max-Age', 60 * 60 * 24 * 365);
}
// intercept OPTIONS method
if (oneof && req.method == 'OPTIONS') {
res.send(200);
}
else {
next();
}
};
appSecure.configure(function(){
appSecure.use(allowCrossDomain);
appSecure.use(express.cookieParser(expressSecret));
appSecure.use(express.bodyParser());
appSecure.use(express.methodOverride());
appSecure.use(org.expressOAuth({onSuccess: '/home', onError: '/oauth/error'})); // <--- nforce middleware
appSecure.set('port', port);
});
appSecure.configure('production', function(){
appSecure.use(express.errorHandler());
appSecure.use(express.session({ secret: expressSecret, store: sessionStore, key:'expressSid', cookie: { domain:'.domain.com'}}));
appSecure.use(passport.initialize());
appSecure.use(passport.session());
appSecure.use(appSecure.router);
appSecure.use(express.static(__dirname + '/public'));
});
答案 0 :(得分:2)
通过清除Chrome中的浏览器Cookie进行修复。
我确实开始将明确的秘密传递给cookieParser,但我认为没有单独解决这个问题。我在早期迭代的请求中传递了几个会话cookie。从浏览器清除cookie修复了该问题。