背景
由于公司內(nèi)部已經(jīng)寫好一套cas系統(tǒng)遭贸,現(xiàn)有一個新的模塊需要基于node并借助原cas系統(tǒng)實(shí)現(xiàn)訪問控制番甩。
本項(xiàng)目基于開源項(xiàng)目
https://github.com/TencentWSRD/connect-cas2
sso原理
快速開始
- 安裝環(huán)境
npm init -y
npm install connect-cas2 --save-dev
//需要node版本7.0及以上
- 構(gòu)建
新建一個js文件client.js寫入
var express = require('express');
var ConnectCas = require('connect-cas2');
var bodyParser = require('body-parser');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var MemoryStore = require('session-memory-store')(session);
var app = express();
app.use(cookieParser());
app.use(session({
name: 'NSESSIONID',
secret: 'Hello I am a long long long secret',
store: new MemoryStore() // or other session store
}));
var casClient = new ConnectCas({
debug: true,
ignore: [
/\/ignore/
],
match: [],
servicePrefix: 'http://localhost:3000',
serverPath: 'http://your-cas-server.com',
paths: {
validate: '/cas/validate',
serviceValidate: '/buglycas/serviceValidate',
proxy: '/buglycas/proxy',
login: '/buglycas/login',
logout: '/buglycas/logout',
proxyCallback: '/buglycas/proxyCallback'
},
redirect: false,
gateway: false,
renew: false,
slo: true,
cache: {
enable: false,
ttl: 5 * 60 * 1000,
filter: []
},
fromAjax: {
header: 'x-client-ajax',
status: 418
}
});
app.use(casClient.core());
// NOTICE: If you want to enable single sign logout, you must use casClient middleware before bodyParser.
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/logout', casClient.logout());
// or do some logic yourself
app.get('/logout', function(req, res, next) {
// Do whatever you like here, then call the logout middleware
casClient.logout()(req, res, next);
});
安裝所有的依賴包
npm install express body-parser express-session cooike-parser session-memory-store --save-dev
- 配置參數(shù)
name: 'NSESSIONID',
secret: 'Hello I am a long long long secret',
控制臺中的application中的cooikes中的key、value填進(jìn)去
servicePrefix: 'http://localhost:3000',
serverPath: 'http://your-cas-server.com',
validate: '/cas/validate',
serverPath:填入cas服務(wù)的地址
validate: '/cas/validate':填入登錄成功后的轉(zhuǎn)跳地址
注意
報錯:
1.express-session錯誤:
express-session deprecated undefined resave option; provide resave option at client.js:14:9
express-session deprecated undefined saveUninitialized option; provide saveUninitialized option at client.js:14:9\
在session中添加兩個參數(shù)
app.use(session({
name: 'xxx',
secret: 'xxx',
resave:true,//添加這行
saveUninitialized: true,//添加這行
store: new MemoryStore() // or other session store
}));
2.cas錯誤
Application Not Authorized to Use CAS
The application you attempted to authenticate to is not authorized to use CAS.
將本服務(wù)的ip地址在服務(wù)端放過或者更改本地hosts文件變成CAS服務(wù)允許的ip地址。