【问题标题】:Basic Auth Laravel4 How to use it?Basic Auth Laravel4 怎么用?
【发布时间】:2014-08-24 10:40:49
【问题描述】:

我尝试使用凭据保护我的 restAPI,并阅读有关基本身份验证 laravel 的信息我尝试实现基本身份验证系统

用户表已经存在并填充了数据

在filter.php中我设置了

Route::filter('auth.basic', function() { 返回 Auth::basic(); });

比在 api 路由中

// =============================================
// API ROUTES ==================================
// =============================================
Route::group(array('prefix' => 'api', 'before' => 'auth.basic'), function() {

            Route::resource('products', 'ProductController', array('only' => array('index', 'store', 'destroy', 'update', 'show', 'edit')));
            Route::get('products/{id}', 'ProductController@get', array('only' => array('show')));
        });

控制器很简单

<?php

use App\Models\Product;

class ProductController extends \BaseController {

    private $model;

    function __construct() {
        $this->model = new Product();
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        $model = new Product();
        $page           =      Input::get('pageNumber');
        $limit          =      Input::get('pageNumber');
        $ram            =      Input::get('limit');
        $cpu            =      Input::get('cpu');
        $price_range    =      Input::get('price_range');
        $keyword       =      Input::get('keyword');
        return Response::json($model->getProducts($page));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {

    }

    public function get($id) {
        $model = new Product();
        return Response::json($model->getProduct($id));
    }

    public function show($id) {       
        return Response::json($this->model->getProduct($id));
    }

    public function update($id) {       
        return Response::json($this->model->getProduct($id));
    }

    public function pause($id) {
        var_dump('pause');
    }

    public function create(){

    }

    public function edit(){
        var_dump('test_edit');
    }

}

调用domain.com/api/products弹出登录窗口。填字段和提交数据无法登录

如何检查用户凭据?

对于后端,我使用 Sentry,它正在工作

过滤器.php

Route::filter('auth.admin', function() {
    if (!Sentry::check()) {
        return Redirect::route('admin.login');
    }
});

路线

Route::get('admin/login', array('as' => 'admin.login', 'uses' => 'App\Controllers\Admin\AuthController@getLogin'));

控制器

<?php namespace App\Controllers\Admin;

use Auth, BaseController, Form, Input, Redirect, Sentry, View;

class AuthController extends BaseController {

    /**
     * Display the login page
     * @return View
     */
    public function getLogin()
    {
        return View::make('admin.auth.login');
    }

    /**
     * Login action
     * @return Redirect
     */
    public function postLogin()
    {
        $credentials = array(
            'email'    => Input::get('email'),
            'password' => Input::get('password')
        );

        try
        {
            $user = Sentry::authenticate($credentials, false);

            if ($user)
            {
                return Redirect::route('admin.pages.index');
            }
        }
        catch(\Exception $e)
        {
            return Redirect::route('admin.login')->withErrors(array('login' => $e->getMessage()));
        }
    }

    /**
     * Logout action
     * @return Redirect
     */
    public function getLogout()
    {
        Sentry::logout();

        return Redirect::route('admin.login');
    }

}

【问题讨论】:

    标签: api laravel-4 basic-authentication


    【解决方案1】:

    您似乎没有定义登录功能。

    顺便说一句,你应该改变:

    Route::group(array('prefix' => 'api', 'before' => 'auth.basic'), function() {
    
                Route::resource('products', 'ProductController', array('only' => array('index', 'store', 'destroy', 'update', 'show', 'edit')));
                Route::get('products/{id}', 'ProductController@get', array('only' => array('show')));
            });
    

    到:

    Route::group(array('prefix' => 'api', 'before' => 'auth.basic'), function(){
    
        Route::get('products/{id}', 'ProductController@get'));
        Route::resource('products', 'ProductController', array('except' => array('show')));
    });
    

    【讨论】:

    • 感谢更正,登录系统已经存在,但后端代码与前端分离
    • 好的。请使用登录/注销功能和路由更新问题,如果您更改了它,请使用过滤器。然后使用 HTML 代码检查检查登录表单是否提交到正确的 URL。
    猜你喜欢
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 2017-08-20
    相关资源
    最近更新 更多