我做了教程,使用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