我搜索了一下,我搜索了很多,终于来到了这里。
有: 我在Bluemix上部署了一个简单的node.js应用程序,它只显示Hello!通过绑定到Bluemix上的应用程序的SSO SAML服务对用户进行身份验证。
需要: 我需要做的是获取用户配置文件(firstName,lastName,displayName,emailId等),以便我可以将用户存储到我选择的数据库(比如Cloudant)并进一步开发应用程序。
问题: 我无法找到检索用户配置文件的方法(示例代码)。我已经读过服务器返回的令牌需要声明/消费,但是没有一个例子可以找到如何做到这一点。
现有主题 stackoverflow上有一个thread,与我的问题相似,但解决方案并不起作用。我的代码解释了这一点。
我的代码:
Strategy = new OpenIDConnectStrategy({
authorizationURL : authorization_url,
tokenURL : token_url,
clientID : client_id,
scope: 'openid',
response_type: 'code',
clientSecret : client_secret,
callbackURL : callback_url,
skipUserProfile: false,
issuer: issuer_id},
function(iss, sub, profile, accessToken, refreshToken, params, done) {
process.nextTick(function() {
profile.accessToken = accessToken;
profile.refreshToken = refreshToken;
done(null, profile);
})
});
passport.use(Strategy);
}
app.get('/',ensureAuthenticated, function(req, res){});
app.get('/login', passport.authenticate('openidconnect', {successRedirect: '/hello',failureRedirect: '/failure'}));
app.get('/hello', function(req, res) {
console.log("Pooshon1: ",JSON.stringify(req.user));
console.log("Pooshon3: ",JSON.stringify(req.profile));
res.send('Hello, ');
//res.send('Hello, '+ req.user.displayName + '!');
//res.send('Hello, '+ req.user['id'] + '!');
});
app.get('/failure', function(req, res) {
res.send('login failed');
});

我没有把整个代码,只是相关内容。因此,护照返回done(null, profile)
,我在互联网上读到的是,此配置文件对象由服务器返回,可以在请求对象中找到。在上面的代码中,在app.get("/hello"
....两个console.log语句下打印"Pooshon: undefined"
,这意味着没有像req.user
或req.profile
那样的最后两个行被注释,因为它会引发内部服务器错误(500)。
如果有人这样做了,请帮助。
答案 0 :(得分:0)
app.get('/', function(req,res,next){
if (!req.isAuthenticated()) {
req.session.originalUrl = req.originalUrl;
res.redirect('/login');
} else {
//If authenticated, continue to next middleware
res.send("You are authenticated : "+JSON.stringify(req.session.passport.user.cn));
res.redirect('/welcome');
}});
我得到了IBM员工Sasaki Rei的回答。答案是,要检索用户配置文件,您需要访问请求对象中包含的会话对象。会话对象包含一个护照对象的实例,而该对象又包含一个'用户'对象和我的朋友包含您可能需要的有关用户的所有信息。我没有提到用户对象内的属性,因为它取决于返回的SSO令牌。在这个例子中,我使用了' cn'属性。您可以打印用户对象以查看您可能需要的其他内容。
要打印:
res.send("You are authenticated : "+JSON.stringify(req.session.passport.user));