我做了教程,使用IoT Foundation Service和.js样板可视化我的树莓派中的数据。Bluemix Single Sign On不显示登录页面
我也跟着下面的教程:
https://developer.ibm.com/recipes/tutorials/visualizing-your-data/
,一切工作正常。
现在我尝试添加单点登录服务进行身份验证。我创建了一个云注册表并添加了两个测试用户。之后,我将服务绑定到我的物联网可视化.js应用程序,并执行将我的应用程序与服务集成的步骤。我跟着官方文档步骤执行这一(点“配置的Node.js应用程序”):
我修改了我的电脑上的文件和使用CL CLI上载他们。问题是,它没有改变任何东西。我可以像以前一样访问我的应用程序,但没有看到任何登录页面。
这里是我的文件:
的package.json
{
"name": "iot-visualization",
"version": "0.1.0",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"passport": "*",
"cookie-parser": "*",
"express-session": "*",
"passport-idaas-openidconnect": "*",
"express": "~4.2.0",
"serve-favicon": "~2.1.0",
"morgan": "~1.0.0",
"cookie-parser": "~1.0.1",
"body-parser": "~1.0.0",
"debug": "~0.7.4",
"jade": "~1.3.0",
"stylus": "0.42.3",
"express-session": "^1.8.1"
}
}
app.js(URL修改)
/*******************************************************************************
* Copyright (c) 2014 IBM Corporation and other Contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM - Initial Contribution
*******************************************************************************/
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var index = require('./routes/index');
var app = express();
var http_host = (process.env.VCAP_APP_HOST || '0.0.0.0');
var http_port = (process.env.VCAP_APP_PORT || 7000);
app.set('port', http_port);
app.set('host',http_host);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
//use favicon
app.use(favicon(__dirname + '/public/images/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
// add session to store the api-key and auth token in the session
app.use(session({secret: 'xxxxxxxxxx',saveUninitialized: true,
resave: true}));
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
var passport = require('passport');
var cookieParser = require('cookie-parser');
var session = require('express-session');
app.use(cookieParser());
app.use(session({resave: 'true', saveUninitialized: 'true' , secret: 'keyboard cat'}));
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// VCAP_SERVICES contains all the credentials of services bound to
// this application. For details of its content, please refer to
// the document or sample of each service.
var services = JSON.parse(process.env.VCAP_SERVICES || "{}");
var ssoConfig = services.SingleSignOn[0];
var client_id = ssoConfig.credentials.clientId;
var client_secret = ssoConfig.credentials.secret;
var authorization_url = ssoConfig.credentials.authorizationEndpointUrl;
var token_url = ssoConfig.credentials.tokenEndpointUrl;
var issuer_id = ssoConfig.credentials.issuerIdentifier;
var callback_url = 'https://xxxxx.mybluemix.net/auth/sso/callback';
var OpenIDConnectStrategy = require('passport-idaas-openidconnect').IDaaSOIDCStrategy;
var Strategy = new OpenIDConnectStrategy({
authorizationURL : authorization_url,
tokenURL : token_url,
clientID : client_id,
scope: 'openid',
response_type: 'code',
clientSecret : client_secret,
callbackURL : callback_url,
skipUserProfile: true,
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('/login', passport.authenticate('openidconnect', {}));
function ensureAuthenticated(req, res, next) {
if(!req.isAuthenticated()) {
req.session.originalUrl = req.originalUrl;
res.redirect('/login');
} else {
return next();
}
}
app.get('/auth/sso/callback',function(req,res,next) {
var redirect_url = req.session.originalUrl;
passport.authenticate('openidconnect', {
successRedirect: redirect_url,
failureRedirect: '/failure',
})(req,res,next);
});
app.get('/failure', function(req, res) {
res.send('login failed'); });
app.use(express.static(path.join(__dirname, 'public')));
app.use('/',index);
app.use(function(req, res, next) {
if(req.session.api_key)
res.redirect("/dashboard");
else
res.redirect('/login');
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
var server = app.listen(app.get('port'), app.get('host'), function() {
console.log('Express server listening on ' + server.address().address + ':' + server.address().port);
});
module.exports = app;
任何想法?
//编辑
有没有.cfignore文件时,我第一次开始教程。所以,我创建了一个在我的应用程序的根文件夹下面的内容:
node_modules/passport
node_modules/cookie-parser
node_modules/express-session
node_modules/passport-idaas-openidconnect
这是正确的,为什么我没有与本教程内容之前有这样的文件:
node_modules
我认为您的问题可能与Bluemix维护在构建之间保留的缓存目录有关。此缓存用于存储解析的依赖关系,因此不必在每次部署时都下载并安装它们。您可能需要覆盖此设置,并通过将NODE_MODULES_CACHE设置为false来禁用它,以便部署更新的依赖项。我已经要求我的同事审查您的帖子并进一步发表评论。 – ValerieLampkin
@ValerieLampkin虽然它确实使用缓存,但Bluemix将始终使用新的package.json运行'npm install'。 NPM识别出缓存缺少新添加的模块并将安装它。无论如何,如果模块不在那里,应用程序会崩溃,说模块不存在。 –
好吧,所以我不需要将NODE_MODULES_CACHE设置为false?我还编辑了关于.cfignore文件的第一篇文章。 – Jenkins