Elastic Beanstalk 不接受来自 Axios 的标头 [英] Elastic Beanstalk doesn't accept headers from Axios

查看:35
本文介绍了Elastic Beanstalk 不接受来自 Axios 的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个以 Lumen API 作为后端的 React Native 应用程序.我想用一个简单的中间件来保护我所有的路由.我修改了 AuthServiceProvider 以检查请求是否具有带有令牌的标头,如果它确实检查该令牌是否属于用户.

I am making a React Native App with a Lumen API as the backend. I want to protect all my routes with a simple middleware. I have modified the AuthServiceProvider to check whether the request has a header with the token, if it does check whether that token belongs to a user.

$this->app['auth']->viaRequest('api', function ($request) {
        if($request->header('access_token')){
            return User::where('access_token', $request->header('access_token'))->first();
        }

        return null;
    });

一个简单的Axios请求看起来像这样,

A simple Axios request would look like this,

axios.get('url/to/site', {headers:{access_token: '12345667890ABC'}})
    .then(res => console.log(res))
    .catch(err => err.response);

路由放置在 web.php 文件中的 auth 中间件内.我知道在使用移动应用程序时我们不必担心 CORS,所以这不是问题.

The routes are placed inside the auth middleware in web.php file. I know that while working with mobile Apps we don't have to worry about CORS, so that isn't the problem.

奇怪的是,代码似乎可以在我的本地主机上运行并对用户进行身份验证,但是当我部署到 AWS ElasticBeanstalk 时,它返回未经授权.我现在正在使用 AWS 的免费套餐.我的实例正在运行 Amazon Linux AMI

Strangely enough the code seems to work on my localhost and authenticates the user, however when I deploy to AWS ElasticBeanstalk it returns unauthorized. I am using the free tier of AWS for now. My instance is running Amazon Linux AMI

我也试过这样设置 axios 标题;

I have also tried setting the axios headers as such;

axios.defaults.headers.common['access_token'] = '12345667890ABC';

为了让您安心,我还通过 Lumen 中的另一个中间件添加了一些额外的标头.

And just for peace of mind I added some additional headers via another middleware in Lumen as well.

    return $next($request)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Headers', 'access_token')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');

我也测试了来自 Postman 的本地和 AWS 路由,无论有没有 access_token.结果是一样的,它适用于本地主机,但不适用于 AWS,其中缺少 header access_token.

I have tested local and AWS routes from Postman as well, both with and without the access_token. The result is the same, it works for localhost but does not work for AWS, where the header access_token is just missing.

起初我认为问题可能是 AWS 上的配置或权限问题,但我也在一个简单的共享托管帐户上尝试了整个过程,但中间件仍然没有通过 Postman 对请求进行身份验证.

At first I thought the problem might be a config or permission issue on AWS but I have also tried the whole process on a simple shared hosting account and the middleware still does not authenticate the request via Postman.

推荐答案

从头变量中去除下划线,例如:- header_var_val = "some value" 替换为 --> headervarval = "some价值"

Remove underscores from the header variables, example:- header_var_val = "some value" replace it with --> headervarval = "some value"

这篇关于Elastic Beanstalk 不接受来自 Axios 的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆