Angularjs CORS遇到Node,Express,Oauth2和Passport问题

时间:2016-04-14 21:18:27

标签: angularjs node.js express oauth cors

最近我们决定将我们的前端从EJS切换到Angular,将前端和后端完全分开。在这样做的过程中,我们开始在多个浏览器中遇到几个问题。在后端,我们使用Node with express以及passport和oauth2。对于前端,我们试图使用角度。 EJS使用express.render工作,但我们更喜欢直接使用angular,将express用作RESTful API。

我在localhost:8080和localhost:3000本地运行后端和前端,尊重。当只使用后端(USING EJS,NOT ANGULAR)时,我可以成功进入浏览器的后端端口,通过passport-oauth登录,然后重定向到我的json数据的帐户页面(来自提供程序登录屏幕)通过res.json呈现。问题是我在删除EJS后无法从前端UI执行此操作。

我尝试使用三种不同的浏览器配置CORS十几种不同的方式,但没有运气。以下三个片段是我尝试通过$ http和$ resource从前端访问localhost:8080时在浏览器控制台中获得的错误(请参阅下面的代码)。三个代码段下面的图像是节点控制台在尝试从每个不同的浏览器访问端口8080时告诉我的内容......

铬:

XMLHttpRequest cannot load 'PROVIDER-DETAILS-URL'. No 'Access-Control-Allow-    Origin' header is present on the requested resource. Origin 'null' is therefore     not allowed access.

火狐:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at 'PROVIDER-DETAILS-URL'. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at 'PROVIDER-DETAILS-URL'. (Reason: CORS request failed).

Safari浏览器:

XMLHttpRequest cannot load http://localhost:8080/auth/PROVIDER. Request header field Accept-Encoding is not allowed by Access-Control-Allow-Headers.

控制台图片 enter image description here

代码:

服务器:

app.js

'use strict';

const express           = require('express');
const session           = require('express-session');
const cookieParser      = require('cookie-parser');
const bodyParser        = require('body-parser');
const logger            = require('morgan');
const errorHandler      = require('errorhandler');
const path              = require('path');
const flash             = require('connect-flash');
const passport          = require('passport');
const expressValidator  = require('express-validator');

/**
 * Load environment variables, where API keys and passwords are configured.
 */
const config = require('./config/config');

/**
 * Route Handlers
 */
const index   = require('./routes/index');
const account = require('./routes/account');
const logout  = require('./routes/logout');

/**
 * API keys and Passport configuration.
 */
const passportConfig = require('./strategy');

/**
 * Create Express server.
 */
const app = express();

/**
 * Express configuration.
 */
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    res.header("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT");
    next();
});
app.use(cookieParser());
app.use(expressValidator());
app.use(session({
    resave              : true,
    saveUninitialized   : true,
    secret              : config.sessionSecret,
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());

/**
 * Primary app routes.
 */
app.get('/', index.execute);
app.get('/account', passportConfig.isAuthenticated, account);
app.get('/logout', logout.execute);

/**
 * OAuth authorization routes.
 */
app.get('/auth/PROVIDER', passport.authenticate('PROVIDER'));
app.get('/auth/PROVIDER/callback', passport.authenticate('PROVIDER', { failureRedirect : '/'}), function(req, res) {
    res.redirect('/account');
});

/**
 * Error Handler.
 */
app.use(errorHandler());

/**
 * Start Express server.
 */
app.listen(8080, () => {
    console.log('App listening on port 8080');
});

module.exports = app;

strategy.js

'use strict';

const passport        = require('passport');
const session         = require('express-session');
const config          = require('./config/config');
const OAuth2Strategy  = require('passport-oauth').OAuth2Strategy;

/**
 * Put together the right header info for PROVIDER
 */
 var authString      = new Buffer(config.PROVIDER.clientID + ':' + config.PROVIDER.clientSecret);
 var customHeader    = {
    "Authorization": "Basic " + authString.toString('base64')
};

/**
 * OAuth2Strategy containing the customHeader created above.
 */
 passport.use('PROVIDER', new OAuth2Strategy({
    authorizationURL    : config.PROVIDER.authorizationURL,
    tokenURL            : config.PROVIDER.tokenURL,
    clientID            : config.PROVIDER.clientID,
    clientSecret        : config.PROVIDER.clientSecret,
    callbackURL         : config.PROVIDER.callbackURL,
    customHeaders       : customHeader,
    passReqToCallback   : true
},
function( req, accessToken, refreshToken, profile, done ) {
    req.session.accessToken = accessToken;
    return done(null, profile); 
}
));

 passport.serializeUser(function(user, done) {
    return done(null, user);
});

 passport.deserializeUser(function(obj, done) {
    return done(null, obj);
});

/**
 * Login Required middleware.
 */
 exports.isAuthenticated = function(req, res, next) {
    if (req.isAuthenticated()) {
        console.log('isAuthenticated');
        return next();
    }
    res.redirect('/');
};

/**
 * Authorization Required middleware.
 */
 exports.isAuthorized = function(req, res, next) {
    var provider = req.path.split('/').slice(-1)[0];
    if (_.find(req.user.tokens, { kind: provider })) {
        next();
    } else {
        res.redirect('/auth/' + provider);
    }
};

index.js

exports.execute = function (req, res) {
    if (req.user) {
        console.log('========== ROUTES/INDEX.JS | 3 ==========');
        res.redirect('/account');
    } else {
        console.log('========== ROUTES/INDEX.JS | 6 ==========');
        res.redirect('/auth/PROVIDER');
    }
};

客户端:

我把它结合起来使它更容易阅读。

angular.module('StackOverflowPost', [])

.factory('APIService', function() {
    function getData( $q, $http ) {
        var defer = $q.defer();
        $http.get( 'localhost:8080' )
            .success( getDataComplete )
            .catch( getDataFailed );

        function getDataComplete( response ) {
            console.log( response.Authorization );
            defer.resolve(response.data.results );
        }

        function getDataFailed( error ) {
            console.log( error.data );
            defer.reject( 'XHR Failed for getData - ' + error.data );
        }
        return defer.promise;
    }
})

.controller('MainCtrl', function( APIService ) {
    var vm = this;

    vm.getDataTest = function() {
        APIService.getData().then(function( returnedData ) {
            console.log( returnedData );
        })
    }
});

非常感谢任何帮助或指示。

更新 (2016年4月28日):我更新了原始帖子的详细信息。我还将代码更新为经过一周的反复试验后的代码。

1 个答案:

答案 0 :(得分:0)

Please check this

https://gist.github.com/dirkk0/5967221

Code should be

// in AngularJS (client)

myApp.config(['$httpProvider', function($httpProvider) {

    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);

// in Express/nodeJS

// in NodeJS/Express (server)
app.all('/*', function(req, res, next) {
   res.header("Access-Control-Allow-Origin", "*");
   res.header("Access-Control-Allow-Headers", "X-Requested-With");
   res.header("Access-Control-Allow-Methods", "GET, POST","PUT");
   next();

});