PassportJS serializeUser和deserializeUser无法正常工作

时间:2016-04-12 21:35:00

标签: node.js passport.js

我正在使用PassportJS来验证我的node.js应用程序。我正在使用WSFED / ADFS策略。但是,我的passport.serializeUser()passport.deserializeUser()功能无效。他们甚至没有被召唤。我找到的典型解决方案是在app.use(passport.initialize())之后添加app.use(passport.session())app.use(session()),我这样做了,我不确定为什么serializeUser()deserializeUser()不是被召唤。这是我的代码:

    var express = require('express'),
        app = express(),
        bodyParser = require('body-parser'),
        cookieParser = require('cookie-parser'),
        session = require('express-session'),
        passport = require('passport'),
        wsfedsaml2 = require('passport-wsfed-saml2').Strategy;

    //Middlewares
    passport.use('wsfed-saml2', new wsfedsaml2({
        realm: 'https://localhost:3001',
        identityProviderUrl: 'https://some_company.org/adfs/ls/',
        thumbprint: '9.....4'
    },
    function(profile, done) {
        console.log(profile);
        return done(null, new User(profile));
    }));

    app.use(cookieParser());

    app.use(bodyParser.urlencoded({
        extended: true
    }));

    app.use(session({
      secret: 'keyboard cat',
      resave: false,
      saveUninitialized: true,
      cookie: { secure: true }
    }));

    app.use(passport.initialize());
    app.use(passport.session());

    passport.serializeUser(function(user, done) {
        console.log('In Serializer'); //does not print
        done(null, user);
    });

    passport.deserializeUser(function(user, done) {
        console.log('In DeSerializer'); //does not print
        done(null, user);
    });

有人可以帮忙吗?

提前致谢!

2 个答案:

答案 0 :(得分:1)

通过删除cookie: { secure: true }解决了同样的问题

app.use(session({
      secret: 'keyboard cat',
      resave: false,
      saveUninitialized: true,
    }));

答案 1 :(得分:0)

编辑:我认为您错误地配置了护照策略。看看文档,看起来应该是:

passport.use(new wsfedsaml2({
    realm: 'https://localhost:3001',
    identityProviderUrl: 'https://some_company.org/adfs/ls/',
    thumbprint: '9.....4'
},
function(profile, done) {
    console.log(profile);
    return done(null, new User(profile));
}));