簡體   English   中英

禁止使用403-通過Passport.io + Express + socket.io跨子域的socket.io連接

[英]403 forbidden - socket.io connection across subdomains with Passport.io + express + socket.io

我在data.domain.com上有一個NodeJS Express服務器,而我的AngularJS客戶端則在subdomain.domain.com上。 我正在使用通行證/快遞在服務器上創建會話。 然后,我的客戶端嘗試連接到同一服務器上的socket.io。 我在socket.io連接上收到403(禁止訪問)。

我認為這是一個跨域問題。 我已經在快遞服務器上啟用了COR。 我正在從服務器data.domain.com中使用TLD設置cookie,即快速cookie域配置為.domain.com。

我檢查了我的會話cookie是否已在TLD .domain.com的客戶端-'expressSid'上設置。 當我注釋掉開始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'));
});

通過在Chrome中清除瀏覽器Cookie修復。

我確實開始將明確的機密傳遞給cookieParser,但我不認為僅此一個問題就可以解決。 我有幾個會話Cookie是根據早期迭代的請求傳遞的。 從瀏覽器清除cookie可以解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM