我想用 passport-saml 库实现 SAML 2.0 SSO,但我不知道在哪里插入它。我尝试遵循此文档:https ://github.com/node-saml/passport-saml但我不知道在哪里插入 saml 策略..
这是我的应用程序:
应用程序.ts:
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import dotenv from 'dotenv';
import Controller from '../controllers/controller';
dotenv.config();
class App {
public app: express.Application;
public port: number;
constructor(controllers: Controller[], port: number) {
this.app = express();
this.port = port;
this.initializeMiddlewares();
this.initializeControllers(controllers);
}
private initializeMiddlewares() {
this.app.use(bodyParser.urlencoded({extended : true}));
this.app.use(cors());
}
private initializeControllers(controllers: Controller[]) {
controllers.forEach((controller: Controller) => {
this.app.use('/', controller.router);
});
}
public listen() {
this.app.listen(this.port, () => {
console.log(`App listening on the port ${this.port}`);
});
}
}
export default App;
控制器.ts:
import { Request, Response, Router } from 'express';
import { GenericErrorResponse, GenericSuccessResponse } from '../models/response/response.model';
class Controller {
public path: string;
public router = Router();
constructor(path: string) {
this.path = path;
}
public generateErrorResponse(status: string, error: any) {
return new GenericErrorResponse(status, error);
}
public generateSuccessResponse(status: string, data: any) {
return new GenericSuccessResponse(status, data);
}
}
export default Controller;
服务器.ts
import App from './app';
import TestController from '../controllers/test.controller';
const app = new App(
[
new TestController('/test'),
],
parseInt(`${process.env.PORT}`, 10) || 3000
);
app.listen();
test.controller.ts(每条路由都在哪里实现)
import { Request, Response } from 'express';
import Controller from './controller';
class TestController extends Controller {
constructor(path: string) {
super(path);
this.intializeRoutes();
}
private intializeRoutes() {
this.router.get(`${this.path}/general`, this.getGeneral);
}
getGeneral = (request: Request, response: Response) => {
response.status(200).json(this.generateSuccessResponse('Success', 'Success'));
}
}
export default TestController;