【问题标题】:Laravel GET Request variables mixed up with urlLaravel GET 请求变量与 url 混淆
【发布时间】:2019-03-28 19:07:15
【问题描述】:

在 Laravel 5.7 Facebook Socialite 登录中观察到 GET 请求的奇怪行为。突然停止工作,这是我发现的:

  1. 在用户点击登录时,使用 facebook 重定向正常工作,返回 code 在我的回调函数的 url 中获取变量:

请求网址:

https://mysite.loc/callback?code=AQD3hC5MTa&state=TvFnYlXwfJsqKm6ZoDbC

Request Method: GET
Status Code: 200 OK

查询字符串参数

code: AQD3hC5MTaJe
state: TvFnYlXwfJsqKm6ZoDbC6I9IaIYWDT4vgvxxx3P4

到目前为止一切顺利,现在到我的回调函数中调试的奇怪东西:

var_dump(\Request::input());

生产:

array (size=2)
'//callback?code' => string 'AQD3hC5MTaJe' (length=344)
'state' => string 'TvFnYlXwfJsqKm6ZoD' (length=40)

Socialite 期望代码在

下可用
 \Request::input('code')

这不是我的情况

\Request::input('//callback?code')

我见过的最奇怪的事情。我在运行 Linux 的 Docker 上使用 NGINX。路线:

Route::get('/redirect', 'SocialAuthFacebookController@redirect');
Route::get('/callback', 'SocialAuthFacebookController@callback');

函数签名:

public function callback(SocialFacebookAccountService $service);

更新

Nginx Vhost 有:

location / {
    try_files $uri $uri/ /index.php?/$request_uri;
}

更新到:

try_files $uri $uri/ /index.php?$request_uri;

这让事情变得更好了,现在调试的结果是:

array (size=2)
'/callback?code' => string 'AQD3hC5MTaJe' (length=344)
'state' => string 'TvFnYlXwfJsqKm6ZoD' (length=40)

注意回调前面少了一个斜线,但它仍然存在......应该只是代码和状态变量。任何人都可以发现我在虚拟主机上做错了什么?看起来库存标准...

我一直想知道如何进一步调试请求失败,显然也不愿意更改社交名流代码。已尝试通过查询和输入访问 - 结果相同...

更新

下面的答案是正确的。编辑 Nginx Vhost 以反映已解决的问题:

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

令人难以置信的是,网上找到的任何使用 Laravel 设置 Nginx 的教程都有错误的设置。很高兴在这里收集这些信息。

【问题讨论】:

    标签: laravel laravel-socialite laravel-request


    【解决方案1】:

    这是请求重定向到 laravel 的 public/index.php 的问题。

    它要么在 nginx 的 vhost 声明中,要么在你的 .htaccess 中

    基本上是https://mysite.loc/callback?code=AQD3hC5MTa&state=TvFnYlXwfJsqKm6ZoDbC

    重定向到

    https://mysite.loc/index.php?//callback?code=AQD3hC5MTa&state=TvFnYlXwfJsqKm6ZoDbC

    而不是

    https://mysite.loc/index.php/callback?code=AQD3hC5MTa&state=TvFnYlXwfJsqKm6ZoDbC

    ps:重定向示例https://mysite.loc/中使用的域名和协议只是为了说明。 nginx 确实做到了这一点

    更新: 这是我在我的 nginx 虚拟主机中放入的内容

    /index.php$is_args$args
    

    【讨论】:

    • 谢谢N69S,投赞成票!发现!唯一的问题是 vhost 清理后仍然存在 1 个正斜杠 - 用清理细节更新了我的问题!
    • @Vlad 在我的回答中添加了一个示例
    猜你喜欢
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 2017-02-17
    • 1970-01-01
    • 2016-08-13
    相关资源
    最近更新 更多