1

私は data.domain.com に NodeJS Express サーバーを持っており、私の AngularJS クライアントは subdomain.domain.com にあります。パスポート/エクスプレスを使用してサーバー上にセッションを作成しています。次に、クライアントが同じサーバー上の socket.io に接続しようとしています。socket.io 接続で 403 (Forbidden) が発生します。

これはクロスドメインの問題だと思います。Express サーバーで COR を有効にしました。サーバー data.domain.com から TLD を使用して Cookie を設定しています。つまり、高速 Cookie ドメイン構成は .domain.com です。

セッション Cookie がクライアントに設定されていることを確認しました - 「expressSid」と TLD .domain.com。io.set("authorization"... で始まるブロックをコメントアウトすると、すべてが即座に機能します。

すべてが HTTPS で実行されています。セッションストレージに RedisStore を使用しています。

Passport.io / socket.io 構成:

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'));
});
4

1 に答える 1