midwayjs / midway

🍔 A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 🌈
https://www.midwayjs.org/
MIT License
7.41k stars 577 forks source link

跨域 #2288

Open rarae opened 2 years ago

rarae commented 2 years ago

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没有跨域头

image
czy88840616 commented 2 years ago

crossDomain 不要放第一个。

wy2022 commented 2 years ago

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没有跨域头 image

不放第一位,解决了吗? 我也遇到这样的问题,后来没办法用nginx处理了

wy2022 commented 2 years ago

image

我的在这个位置 也是跨域

rarae commented 2 years ago

crossDomain 不要放第一个。

试了放在其他地方都不能跨域

rarae commented 2 years ago

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没有跨域头 image

不放第一位,解决了吗? 我也遇到这样的问题,后来没办法用nginx处理了

不能。

wy2022 commented 2 years ago

我看 社区 有人 可以说,可以做一个中间件,去处理

wy2022 commented 2 years ago
// 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();
      // 控制器之后执行的逻辑
    };
  }
}

试试这个

echosoar commented 2 years ago

配置 credentials 为 true 的时候,不能配置 origin 为 * ref: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Access-Control-Allow-Origin#%E6%8C%87%E4%BB%A4

rarae commented 2 years ago

配置 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;`

这样配置也不行

eijil commented 1 year ago

没人遇到这个问题吗? 本地可以跨域我上线就不行了,响应头也没有信息,是插件问题吗?

365code365 commented 8 months ago

async onReady() { this.app.useMiddleware(require('@koa/cors')()) } 用这个方法

airportdc commented 1 month ago

和middlewa 的顺序也有关系, 我排查了以上原因后 , 去掉了 this.app.getMiddleware().insertFirst(JwtPassportAuthMiddleware); 生效

czy88840616 commented 1 month ago

和middlewa 的顺序也有关系, 我排查了以上原因后 , 去掉了 this.app.getMiddleware().insertFirst(JwtPassportAuthMiddleware); 生效

所以推荐一般无必要,只用 useMiddleware,不要改顺序