繁体   English   中英

为什么 CORS 未在 Nestjs 应用程序中应用

[英]Why is CORS not being applied in Nestjs applicaiton

我有 Nestjs 应用程序的这个设置

const globalPrefix = 'api';
  const app = await NestFactory.create(AppModule);
  const config = app.get<ConfigService>(ConfigService);
  const port = config.get('API_PORT');
  const allowedCors = config.get('CORS_ORIGINS').split(',');

  app.enableCors({ origin: allowedCors });
  app.setGlobalPrefix(globalPrefix);
  app.useGlobalPipes(new ValidationPipe());
  app.use(helmet());
  app.use(compression());
  app.use(cookieParser());
  // app.use(csurf({ cookie: true }));
  app.use(
    rateLimit({
      windowMs: 15 * 60 * 1000,
      max: config.get('ENV') === 'development' ? Infinity : 100,
    }),
  );

允许的 cors 只是前端应用程序的 1 个链接,但每当我从另一个来源尝试它时,它都会通过。 我究竟做错了什么?

响应头是这样的:

HTTP/1.1 404 Not Found
Server: nginx/1.18.0 (Ubuntu)
Date: Sun, 25 Sep 2022 14:44:13 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 241
Connection: keep-alive
Vary: Origin, Accept-Encoding
Content-Security-Policy: default-src 'self';base-uri 'self';block-all-mixed-content;font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Resource-Policy: same-origin
X-DNS-Prefetch-Control: off
Expect-CT: max-age=0
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=15552000; includeSubDomains
X-Download-Options: noopen
X-Content-Type-Options: nosniff
Origin-Agent-Cluster: ?1
X-Permitted-Cross-Domain-Policies: none
Referrer-Policy: no-referrer
X-XSS-Protection: 0
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 97
X-RateLimit-Reset: 1664117909

它显然在 postman 中通过,但是当我从浏览器发出 GET 请求时它也通过(只需复制并粘贴 get 请求的 url)

我想你可能误解了 CORS。 Postman 不呈现任何交互式脚本,当您在浏览器地址栏中发布 URL 时,该请求不是跨域请求。

CORS 不适用于您主动发起的直接 GET 请求。 CORS 是一种浏览器屏障,用于限制浏览器中的跨域请求。

由于所有内容都设置为same-origin ,这意味着只有您自己的站点可以通过浏览器发起的请求访问您自己的站点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM