5

我用 SLIM 微框架构建了一个 API。我使用以下代码设置了一些添加 CORS 标头的中间件。

class Cors{

    public function __invoke(Request $request, Response $response, $next){

        $response = $next($request, $response);
        return $response
            ->withHeader('Access-Control-Allow-Origin', 'http://mysite')
            ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
            ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }

}

对于我的前端,我使用了 VueJS。我设置了 VueResource 并使用以下代码创建了一个函数。

register (context, email, password) {
  Vue.http({
    url: 'api/auth/register',
    method: 'POST',
    data: {
    email: email,
    password: password
  }
}).then(response => {
  context.success = true
}, response => {
  context.response = response.data
  context.error = true
})
}

在 chrome 中,控制台会记录以下错误。

XMLHttpRequest cannot load http://mysite:9800/api/auth/register. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mysite' is therefore not allowed access.

奇怪的是,GET 请求可以完美运行。

4

4 回答 4

2

你在这里解决了一半的问题。

您缺少的是一个 OPTIONS 路由,其中​​还需要添加这些标头。

$app->options('/{routes:.+}', function ($request, $response, $args) {
    return $response
        ->withHeader('Access-Control-Allow-Origin', 'http://mysite')
        ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
        ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
});
于 2016-06-26T04:06:56.803 回答
1

发生这种情况是因为预检请求属于 OPTIONS 类型。您需要在您的请求上创建一个事件侦听器,它会检查类型并发送带有所需标头的响应。

不幸的是,我不知道 Slim 框架,但这是 Symfony 中的工作示例。

首先要返回的标题示例:

// Headers allowed to be returned.
const ALLOWED_HEADERS = ['Authorization', 'Origin', 'Content-Type', 'Content-Length', 'Accept'];

在请求监听器中,有一个 onKernelRequest 方法可以监视所有进来的请求:

    /**
     * @param GetResponseEvent $event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        // Don't do anything if it's not the master request
        if (!$event->isMasterRequest()) {
            return;
        }

        // Catch all pre-request events
        if ($event->getRequest()->isMethod('OPTIONS')) {
            $router = $this->container->get('router');
            $pathInfo = $event->getRequest()->getPathInfo();

            $response = new Response();
            $response->headers->set('Access-Control-Allow-Origin', $event->getRequest()->headers->get('Origin'));
            $response->headers->set('Access-Control-Allow-Methods', $this->getAllowedMethods($router, $pathInfo));
            $response->headers->set('Access-Control-Allow-Headers', implode(', ', self::ALLOWED_HEADERS));
            $response->headers->set('Access-Control-Expose-Headers', implode(', ', self::ALLOWED_HEADERS));
            $response->headers->set('Access-Control-Allow-Credentials', 'true');
            $response->headers->set('Access-Control-Max-Age', 60 * 60 * 24);
            $response->send();
        }
    }

在这里,我只是复制了 Origin(所有域都可以请求资源,您可能应该将其更改为您的域)。希望它会给一些胶水。

于 2016-06-24T05:44:50.513 回答
1

实际上 CORS 是在浏览器级别实现的。甚至有

 return $response
            ->withHeader('Access-Control-Allow-Origin', 'http://mysite')
            ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
            ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');

chrome 和 Mozilla 不会设置标题以允许跨源。所以,你需要强行禁用它..

阅读有关禁用 CORS 的更多信息

在 Chrome 中禁用同源策略

于 2016-06-24T06:17:28.587 回答
1

CORS 可能很难配置。关键是您需要在服务器和客户端中设置特殊标头,而且我没有看到任何 Vue 标头设置,除了据我所知http不是函数。但是,这里是发布请求的一些设置。

const data = {
    email: email,
    password: password
  } 
const options = {
    headers: {
        'Access-Control-Expose-Headers': // all of your headers,
        'Access-Control-Allow-Origin': '*'
    }
}
Vue.http.post('api/auth/register', JSON.stringify(data), options).then(response => {
    // success
}, response => {
    // error
})

请注意,您需要对数据进行字符串化,并且需要公开标头,通常包括Access-Control-Allow-Origin标头。我在自己的一个应用程序中所做的是定义拦截器,因此我不必担心为每个请求设置标头。

Vue.http.headers.common['Access-Control-Expose-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, x-session-token, timeout, Content-Length, location, *'
Vue.http.headers.common['Access-Control-Allow-Origin'] = '*'
于 2016-06-24T15:54:14.780 回答