我为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只是前端应用程序的一个链接,但是每当我从另一个来源尝试它时,它就会通过。我做错了什么?
响应头如下所示:
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发布于 2022-09-25 14:58:49
显然是在邮递员中完成的,但当我从浏览器发出GET请求时(只需复制和粘贴get请求的url ),它也会通过。
我想你可能误解了CORS。Postman不呈现任何交互式脚本,当您在浏览器地址栏中发布URL时,该请求不是跨域请求。
CORS不适用于直接主动发起的GET请求。CORS是限制浏览器中跨域请求的浏览器屏障.
因为所有内容都设置为same-origin,这意味着只有您自己的站点可以通过浏览器启动的请求访问您自己的站点。
https://stackoverflow.com/questions/73845274
复制相似问题