简体   繁体   English

请求头字段授权是不允许的

[英]Request header field Authorization is not allowed

I have an Laravel based API on my Server.我的服务器上有一个基于 Laravel 的 API。 And I try to access this api from an AngularJS Frontend.我尝试从 AngularJS 前端访问这个 api。 Unfortunately this error shows up.不幸的是,这个错误出现了。

Where do I have to add the configuration to allow the Authorization Header?我必须在哪里添加配置以允许授权标头? I tried to find a solution but couldn't manage to solve it.我试图找到解决方案,但无法解决。

I tried to insert it in the httpd.conf and in the .htaccess of the Angular frontend but unfortunately it didn't work:我试图将它插入到httpd.conf和 Angular 前端的.htaccess中,但不幸的是它没有工作:

Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.

Quick Answer快速回答

If your server is using Apache, add the following snippet to your api's respective .htaccess file:如果您的服务器使用 Apache,请将以下代码段添加到您的 api 各自的.htaccess文件中:

<IfModule mod_headers.c>
          Header set Access-Control-Allow-Headers "Authorization"
</IfModule>

More details更多细节

in the above snippet, we are asking Apache to allow Authorization header:在上面的代码段中,我们要求 Apache 允许Authorization标头:

Header set Access-Control-Allow-Headers "Authorization"

you can add multiple headers in one line, like this, but keep in mind that it's advised to only allow the headers required by your app .您可以像这样在一行中添加多个headers ,但请记住,建议仅允许您的应用程序所需的标题

Header set Access-Control-Allow-Headers "Authorization, X-Requested-With"

Side Note边注

In a related note, people often need to allow cross-origin request (also known as Cross-Origin Resource Sharing or CORS).在相关说明中,人们通常需要允许跨域请求(也称为跨域资源共享或 CORS)。 The following snippet allows cross requests from https://example.com origin:以下代码段允许来自https://example.com源的交叉请求:

Header set Access-Control-Allow-Origin "https://example.com"

or you might use "*" to allow requests from all origins: Not Recommended或者您可以使用"*"来允许来自所有来源的请求:不推荐

Header set Access-Control-Allow-Origin "*"

Wrap up包起来

<IfModule mod_headers.c>
    # To allow headers
    Header set Access-Control-Allow-Headers "Authorization"

    # to allow cross origin request from https://example.com
    Header set Access-Control-Allow-Origin "https://example.com"
</IfModule>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 请求标头字段Cache-Control不允许 - Request header field Cache-Control is not allowed 请求标头字段不允许Access-Control-Allow-Origin - Request header field Access-Control-Allow-Origin is not allowed PHP-标头授权请求 - PHP - Header authorization request 为什么我不能通过$ this-&gt; request-&gt; header(&#39;Authorization&#39;)访问标头字段? - Why can't I access header-field via $this->request->header('Authorization')? 离子应用程序中的错误“飞行前响应中Access-Control-Allow-Headers不允许请求标头字段。” - Error in ionic app “Request header field is not allowed by Access-Control-Allow-Headers in preflight response.” 请求标头字段Access-Control-Allow-Headers在预检响应中不允许使用Access-Control-Allow-Headers - Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers in preflight response Access-Control-Allow-Headers 不允许请求头字段 X-Requested-With。 (CORS) - Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers. (CORS ) 飞行响应中的 Access-Control-Allow-Headers 不允许请求标头字段 Content-Type - Request header field Content-Type is not allowed by Access-Control-Allow-Headers in flight response 从休假请求中获取授权标头 - get Authorization Header from Recess Request Guzzle HTTP - 将授权标头直接添加到请求中 - Guzzle HTTP - add Authorization header directly into request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM