简体   繁体   English

使用自定义路由的Laravel 5.2身份验证

[英]Laravel 5.2 authentication with custom routes

I'm working in a web project using Laravel 5.2 我正在使用Laravel 5.2在Web项目中工作

My use case is very simple: Before rendering any page, verify if the user is authenticated. 我的用例非常简单:在呈现任何页面之前,请验证用户是否已通过身份验证。 If not, provide a login form with custom authentication (not the Eloquent's default stuff) 如果没有,请提供带有自定义身份验证的登录表单(不是Eloquent的默认内容)

After reading about this scenario I have: 在了解了这种情况之后,我有:

// routes.php
Route::get('/', 'HomeController@index');

Then if I want all my pages secured, I require the middleware auth in controller's constructor. 然后,如果我想保护我的所有页面,则需要在控制器的构造函数中使用中间件auth All my controllers should follow the same pattern if I want to request a logged in user before serving any page. 如果我要在提供任何页面之前请求登录用户,则我所有的控制器都应遵循相同的模式。

// app/Http/Controllers/HomeController.php
<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;

class HomeController extends Controller
{
  public function __construct()
  {
      $this->middleware('auth');
  }

  public function index()
  {
      return view('home');
  }
}

So far, so good. 到现在为止还挺好。 If I visit my app at '/' I'm redirected to a /login page. 如果我在“ /”处访问我的应用程序,则会重定向到/ login页面。

I created the login page: 我创建了登录页面:

// views/auth/login.blade.php
@extends('layouts.app')

@section('content')
    <form class="form-signin" method="post" action="{{ url ('/login') }}">
        {!! csrf_field() !!}
        <h2 class="form-signin-heading">Please sign in</h2>
        <label for="inputUsername" class="sr-only">Username</label>
        <input type="text" id="inputUsername" class="form-control" placeholder="Username" name="username" required autofocus>
        <label for="inputPassword" class="sr-only">Password</label>
        <input type="password" id="inputPassword" class="form-control" placeholder="Password" name="password" required>        
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
    </form>

@endsection

Please notice the action of the form that redirects to /login. 请注意重定向到/ login的表单的操作。 Then I update the routes.php by providing the new following routes: 然后,通过提供以下新路由来更新routes.php:

// Authentication Routes...
Route::get('login', 'Auth\AuthController@showLoginForm');
Route::post('login', 'Auth\AuthController@login');
Route::get('logout', 'Auth\AuthController@logout');

Route::get('/', 'HomeController@index');

With these new routes I'm catching login/logout scenarios and assigning AuthController's methods to handle them. 使用这些新路由,我可以捕获登录/注销场景并分配AuthController的方法来处理它们。

On the already implemented AuthController, I guess I need to define these methods. 在已经实现的AuthController上,我想我需要定义这些方法。

I was not able to make this to work or maybe I'm doing this custom authentication in a wrong way. 我无法使其正常工作,或者我以错误的方式进行了此自定义身份验证。

I have: 我有:

// app/Http/Auth/AuthController.php
<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    protected $redirectTo = '/';

    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    // ... lot of default stuff ..

    protected function login($data)
    {
        //
        // do custom login authentication with $data
        // ie: validate thru a web service or something
        //

        return redirect()->intended('/');
    }
}

Any suggestions on how to implement this? 关于如何实施此建议?

Change your login function to 将您的登录功能更改为

protected function login(Request $data)
{
    //
    // do custom login authentication with $data
    // ie: validate thru a web service or something
    //

    return redirect()->intended('/');
}

This overrides the function from Illuminate\\Foundation\\Auth\\AuthenticatesUsers that originally used by Laravel Auth 这会覆盖Laravel Auth最初使用的Illuminate\\Foundation\\Auth\\AuthenticatesUsers中的功能

Thanks to everyone who responded to this question. 感谢所有回答此问题的人。 I ended rolling out a custom integration where users are duplicated on the Eloquent's User model and my external service. 我结束了一个自定义集成,其中在Eloquent的用户模型和我的外部服务上复制了用户。

Unfortunately the lack of documentation around Laravel 5.2 and custom authentication methods makes this as a patch solution until something more stable comes out. 不幸的是,缺少有关Laravel 5.2和自定义身份验证方法的文档,这使得它成为一个补丁解决方案,直到出现更稳定的情况为止。

Thanks and happy coding! 谢谢,祝您编程愉快!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM