0

我需要一些帮助来使用护照-saml 实现 SAML 2.0 SSO,它使用了不同的策略来实现应用程序:(不是我的策略,我不太习惯 nodejs,我阅读了所有可能的文档,例如:https:// github.com/node-saml/passport-saml/blob/master/README.md 但我不知道如何使其适应此代码)

应用程序.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;
4

0 回答 0