【问题标题】:Laravel - Api requestLaravel - Api 请求
【发布时间】:2014-10-24 14:49:07
【问题描述】:

我有一个使用 Laravel 4.2 构建的网站和一个只有登录用户才能访问的控制器:

Route::group(['prefix' => 'pannello-utente', 'before' => 'auth'], function ()
{
    Route::get('elenco-animali/{nome?}', ['uses' => 'PazientiController@index']);
    Route::get('download/{cartella}/{file}', ['uses' => 'PazientiController@download']);
});

auth 方法要求用户输入电子邮件和密码。

PazientiController 中,我有__construct 方法,它执行一些检索代码并在其他方法中使用它的查询:

public function __construct()
{
    $this->current_user = Auth::user();

    //Recupero il codice cliente salvato nel profilo utente
    $this->codice = User::find($this->current_user->id)->first()->profile->codice_cliente;
}

此代码仅保存在数据库中,目前没有其他应用程序拥有它,因此我需要每次都执行该查询。

我需要,现在通过curl然后通过智能手机应用程序,用户可以访问该方法,所以我需要用户之前登录:

curl --user chri788@gmail.com:adminadmin localhost:8888/*****/public/pannello-utente/elenco-animali

但我明白了:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="refresh" content="1;url=http://localhost:8888/*****/public/login" />

        <title>Redirecting to http://localhost:8888/*****/public/login</title>
    </head>
    <body>
        Redirecting to <a href="http://localhost:8888/******/public/login">http://localhost:8888/****/public/login</a>.
    </body>
</html>

如何访问该控制器?

【问题讨论】:

    标签: api curl laravel-4


    【解决方案1】:

    curl 的 --user 标志触发 HTTP 基本身份验证。 Laravel 支持这一点,但不支持 auth 过滤器 - 它需要 auth.basic 过滤器。

    您可以编写一个自定义过滤器来检查authauth.basic,或者您可以提供一组使用备用过滤器的单独路由(可能以api 为前缀)。

    http://laravel.com/docs/4.2/security#http-basic-authentication

    【讨论】:

    • @ChristianGiupponi 是的,应该这样做。您还可以构建一个适当的 API 密钥系统,这样您就不会传递用户名/密码(尤其是在您的密码更改时)
    猜你喜欢
    • 2019-01-06
    • 2016-03-07
    • 2017-10-01
    • 2021-02-21
    • 2018-09-13
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2021-01-07
    相关资源
    最近更新 更多