设置使用CAS进行身份验证

时间:2018-01-30 13:55:27

标签: node.js reactjs express cas koa2

我正在尝试创建一个使用CAS(中央身份验证服务)的react-app来对某些URL进行身份验证。我使用express来使用CAS客户端库来运行服务器 koa-cas2 https://www.npmjs.com/package/koa-cas2

这是我运行服务器的server.js文件。

import express from 'express';
import path from 'path';
import open from 'open';
import compression from 'compression';
import ConnectCas from 'koa-cas2';
import bodyParser from 'body-parser';
import session from 'express-session';
import cookieParser from 'cookie-parser';
const MemoryStore  =  require('session-memory-store')(session);

const port = 3000;
const app = express();

app.use(compression());
app.use(cookieParser());

app.use(express.static('build'));

app.get('*', function (req, res) {
    res.sendFile(path.join(__dirname, '../build/index.html', ));
});

app.use(session({
    name: 'NSESSIONID',
    secret: 'Hello I am a long long long secret',
    store: new MemoryStore(),// or other session store 
    saveUninitialized: false,
    resave: false
}));

var casClient = new ConnectCas({
    debug: true,
    ignore: [
        /\/ignore/
    ],
    match: [],
    servicePrefix: 'http://localhost:3000',
    serverPath: 'http://<myCasServerIp>:8080/',
    paths: {
        validate: '/cas/validate',
        serviceValidate: '/cas/serviceValidate',
        proxy: '/cas/proxy',
        login: '/cas/login',
        logout: '/cas/logout',
        proxyCallback: '/cas/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);
});

app.listen(port, function (err) {
    if (err) {
        console.log(err);
    } else {
        open(`http://localhost:${port}`);
    }
});

我不确定如何在我的React应用程序中正确使用它。如果有人有这方面的经验,请分享他/她的经验,并告诉正确的方法。

0 个答案:

没有答案