Open rarae opened 2 years ago
crossDomain 不要放第一个。
configuration.ts 文件
import { Configuration, App } from '@midwayjs/decorator'; import * as koa from '@midwayjs/koa'; import * as validate from '@midwayjs/validate'; import * as info from '@midwayjs/info'; import * as grpc from '@midwayjs/grpc'; import { join } from 'path'; // import { DefaultErrorFilter } from './filter/default.filter'; // import { NotFoundFilter } from './filter/notfound.filter'; import { ReportMiddleware } from './middleware/report.middleware'; import * as crossDomain from '@midwayjs/cross-domain'; @Configuration({ imports: [ crossDomain, koa, grpc, validate, { component: info, enabledEnvironment: ['local'], }, ], importConfigs: [join(__dirname, './config')], }) export class ContainerLifeCycle { @App() app: koa.Application; async onReady() { // add middleware this.app.useMiddleware([ReportMiddleware]); // add filter // this.app.useFilter([NotFoundFilter, DefaultErrorFilter]); } } ` config.default.ts文件 `import { MidwayConfig } from '@midwayjs/core'; export default { // use for cookie sign key, should change to your own and keep security keys: '1661168979375_248', koa: { port: 7001, }, cors: { credentials: true, origin: '*', allowHeaders: '*', allowMethods: '*', }, } as MidwayConfig;
为何response没有跨域头
不放第一位,解决了吗? 我也遇到这样的问题,后来没办法用nginx处理了
我的在这个位置 也是跨域
crossDomain 不要放第一个。
试了放在其他地方都不能跨域
configuration.ts 文件
import { Configuration, App } from '@midwayjs/decorator'; import * as koa from '@midwayjs/koa'; import * as validate from '@midwayjs/validate'; import * as info from '@midwayjs/info'; import * as grpc from '@midwayjs/grpc'; import { join } from 'path'; // import { DefaultErrorFilter } from './filter/default.filter'; // import { NotFoundFilter } from './filter/notfound.filter'; import { ReportMiddleware } from './middleware/report.middleware'; import * as crossDomain from '@midwayjs/cross-domain'; @Configuration({ imports: [ crossDomain, koa, grpc, validate, { component: info, enabledEnvironment: ['local'], }, ], importConfigs: [join(__dirname, './config')], }) export class ContainerLifeCycle { @App() app: koa.Application; async onReady() { // add middleware this.app.useMiddleware([ReportMiddleware]); // add filter // this.app.useFilter([NotFoundFilter, DefaultErrorFilter]); } } ` config.default.ts文件 `import { MidwayConfig } from '@midwayjs/core'; export default { // use for cookie sign key, should change to your own and keep security keys: '1661168979375_248', koa: { port: 7001, }, cors: { credentials: true, origin: '*', allowHeaders: '*', allowMethods: '*', }, } as MidwayConfig;
为何response没有跨域头
不放第一位,解决了吗? 我也遇到这样的问题,后来没办法用nginx处理了
不能。
我看 社区 有人 可以说,可以做一个中间件,去处理
// src\middleware\cors.ts
import { Provide } from '@midwayjs/decorator';
import { IWebMiddleware, IMidwayWebNext } from '@midwayjs/web';
@Provide()
export class CorsMiddleware implements IWebMiddleware {
resolve() {
return async (ctx: Context, next: IMidwayWebNext) => {
// 控制器前执行的逻辑
ctx.response.set('Access-Control-Allow-Origin', '*');
ctx.response.set('Access-Control-Allow-Methods', 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS');
// 执行下一个 Web 中间件,最后执行到控制器
await next();
// 控制器之后执行的逻辑
};
}
}
试试这个
配置 credentials 为 true 的时候,不能配置 origin 为 * ref: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Access-Control-Allow-Origin#%E6%8C%87%E4%BB%A4
配置 credentials 为 true 的时候,不能配置 origin 为 * ref: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Access-Control-Allow-Origin#%E6%8C%87%E4%BB%A4
`import { MidwayConfig } from '@midwayjs/core';
export default { // use for cookie sign key, should change to your own and keep security keys: '1661168979375_248', koa: { port: 7001, }, cors: { credentials: false, origin: '*', }, } as MidwayConfig;`
这样配置也不行
没人遇到这个问题吗? 本地可以跨域我上线就不行了,响应头也没有信息,是插件问题吗?
async onReady() { this.app.useMiddleware(require('@koa/cors')()) } 用这个方法
和middlewa 的顺序也有关系, 我排查了以上原因后 , 去掉了 this.app.getMiddleware().insertFirst(JwtPassportAuthMiddleware); 生效
和middlewa 的顺序也有关系, 我排查了以上原因后 , 去掉了 this.app.getMiddleware().insertFirst(JwtPassportAuthMiddleware); 生效
所以推荐一般无必要,只用 useMiddleware,不要改顺序
configuration.ts 文件
为何response没有跨域头