file module import { HttpStatus , Injectable , NestMiddleware } from '@nestjs/common' ; import { Request , Response } from 'express' ; import { CROSS_DOMAIN } from '../app.config' ; import { isProdMode } from '../app.environment' ; import * as TEXT from '../common/constants/text.constant' ; import { EHttpStatus , THttpErrorResponse } from '../common/types/interfaces/http.interface' ; class classdesc @ Injectable ( ) export class OriginMiddleware implements NestMiddleware { use ( request , response , next ) { if ( isProdMode ) { console . log ( '生产环境' ) ; const { origin , referer } = request . headers ; const checkHeader = ( field ) => ! field || field . includes ( CROSS_DOMAIN . allowedReferer ) ; const isVerifiedOrigin = checkHeader ( origin ) ; const isVerifiedReferer = checkHeader ( referer ) ; if ( ! isVerifiedOrigin && ! isVerifiedReferer ) { return response . status ( HttpStatus . UNAUTHORIZED ) . jsonp ( { status : EHttpStatus . Error , message : TEXT . HTTP_ANONYMOUSE_TEXT , error : null , } as THttpErrorResponse ) ; } } return next ( ) ; } }