laravel JWT:如何在标头中发送授权?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28681735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:00:34  来源:igfitidea点击:

JWT: How send authorization in header?

phprestlaraveltokenjwt

提问by Valdinei

I'm using the JWT (https://github.com/tymondesigns/jwt-auth) to generate session tokens in my API.

我正在使用 JWT ( https://github.com/tymondesigns/jwt-auth) 在我的 API 中生成会话令牌。

I made all relevant settings to work as the author's documentation.

我进行了所有相关设置以作为作者的文档。

After connecting the session, I make use of a URL to return data of my categories. When I pass the token directly in the URL, it works. As follows:

连接会话后,我使用 URL 返回我的类别数据。当我直接在 URL 中传递令牌时,它可以工作。如下:

http://api.domain.com/categories?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9kZXYuaW1pYXZpcFwvYXBpXC9hdXRoXC9hdXRoZW50aWNhdGUiLCJzdWIiOiIxIiwiaWF0IjoxNDIxODQyMzU4LCJleHAiOjE0MjE5Mjg3NTh9.-nqKoARKc2t1bI2j5KFEP_zRU8KCki_dghKe6dtAedY

Only I need to pass the token, in my request on the header, using the Authentication Bearer. But does not work. See how I'm going through:

只有我需要在我对标头的请求中使用身份验证承载传递令牌。但不起作用。看看我是怎么经历的:

Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9kZXYuaW1pYXZpcFwvYXBpXC9hdXRoXC9hdXRoZW50aWNhdGUiLCJzdWIiOiIxIiwiaWF0IjoxNDIxODQyMzU4LCJleHAiOjE0MjE5Mjg3NTh9.-nqKoARKc2t1bI2j5KFEP_zRU8KCki_dghKe6dtAedY

What could be wrong?

可能有什么问题?

In the JWT documentation mentions the use of the form I spent above. But does not work.

在 JWT 文档中提到了我上面使用的表单的使用。但不起作用。

采纳答案by tymondesigns

If you are using Apache, then the headers are probably not coming through, due to this known issuewithin Symfony

如果您使用的是 Apache,那么由于Symfony 中的这个已知问题,标头可能无法通过

You will need to add the following to your virtual host if this is the case:

如果是这种情况,您需要将以下内容添加到您的虚拟主机中:

RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

回答by aquasmit

Please make sure that you have below line in your backend (server side) code This is for PHP.

请确保您的后端(服务器端)代码中有以下行 这是针对 PHP 的。

header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization');

Notice that with above line we are allowing "Authorization" header.

请注意,在上面的行中,我们允许“授权”标题。

Once you have above line in your server side code, then you can you below function (if you are coding in php) to get all headers in array.

一旦你的服务器端代码中有上面的行,那么你就可以在函数下面(如果你在 php 中编码)来获取数组中的所有标题。

getallheaders();