laravel 搭建oauth2認證服務,可用于app登錄等驗證

oauth2

oauth2是一種驗證方式, 這里不加以解釋,不明白的小伙伴可以看一看阮一峰的文章-理解oauth2.0

使用的庫

https://github.com/lucadegasperi/oauth2-server-laravel

  • 按github上的教程搭建oauth2認證
    先進行composer安裝然后配置,數(shù)據(jù)遷移。
  • 選擇一種驗證方式
    oauth2-server-laravel 支持Client Credentials Grant,password等四種驗證方式,可按照教程在config/oauth2.php下配置,例如選擇password granttype,就可按照https://github.com/lucadegasperi/oauth2-server-laravel/blob/master/docs/authorization-server/choosing-grant.md操作。
  • 登錄驗證(這里使用密碼方式)
    現(xiàn)在oauth_clients表中新建一條數(shù)據(jù),然后每次登錄驗證時就發(fā)送參數(shù)client_id和client_secret; 在User.php修改驗證的用戶表
protected $table="ims_mc_members"; 

在routes.php新建以下路由

Route::post('oauth/access_token', function() {
return Response::json(Authorizer::issueAccessToken());
});

表單請求上面的路由,發(fā)送表單數(shù)據(jù)

grant_typ = "password" 
client_id = "your_client_id"
client_secret = "your_client_secret" 
username = "you_email@xxx.com" 
password = "your_password",

注意2點,
1.按照自己寫的restapi格式發(fā)送表單,post或者get的話指定action就好,其他如delete,patch,put等就需要對應的在表單中加上一個隱藏的input如:

_method = 'delete'

2.默認用郵箱登錄
以上就搭建完了,等等,如果我登錄不用郵箱怎么辦,如果我用了salt加密怎么辦, 那往下看吧。

自定義驗證規(guī)則

有時候可能不是用郵箱登錄,或者驗證方式與上面的不同,比如我的系統(tǒng)使用md5+salt驗證,mobile和email都可登錄,這時候就要修改默認驗證方式了

  1. 修改app下PasswordGrantVerifier.php的verify.php方法
public function verify($username, $password)
{
//harry 修改驗證 anasit
      $credentials = [
        'uniacid' => $_REQUEST['uniacid'],
        'password' => $password,
    ];
    if(strpos($username, '@')){
        $credentials['email'] = $username;
    }else{
        $credentials['mobile'] = $username;
    }

    if (Auth::once($credentials)) {
        return Auth::user()->id;
    }

    return false;
}

因為之前會正則驗證郵箱和手機號,所以就偷了個懶,用是否含有@來區(qū)分郵箱和手機號了。

  1. 這樣就修改成mobile和email都可登錄了
    哎,上面的uniacid是什么鬼?
    uniacid是因為在做微信開發(fā),識別不同公眾號,比如權(quán)限管理,管理員和用戶都在一個user表中,一個人可以是管理員也可以是用戶,他的賬號密碼可能相同,只是一個的role(角色)是user,一個是admin,這樣你就可以在$credentials,加入一個鍵值對'role'=>'admin'或'role'=>'user'來做驗證。
    但是接下來還要修改驗證方式,
    驗證方法在/vendor/laravel/frameword/src/Illuminate/Auth/EloquentUserProvider.php里面的validateCredentials方法可以自定義自己的驗證邏輯md5,hash或者其他,我的修改為
  public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['password'];
$salt =$user->getAuthSalt();
$upwd = md5($plain.$salt.config('app.authkey'));
return $user->getAuthPassword() == $upwd;
}

getAuthPassword是獲取用戶表中的password,但是我還要salt驗證,就需要新建一個getAuthSalt()方法取得用戶表中的salt,需要在\vendor\laravel\framework\src\Illuminate\Auth\Authenticatable.php,\vendor\laravel\framework\src\Illuminate\Auth\GenericUser.php,\vendor\laravel\framework\src\Illuminate\Contracts\Auth\Authenticatable.php,是不是好多文件不好找,其實在vendor文件下搜索一下getAuthPassword就基本知道要修改哪些文件了,
修改好的是這樣的
\vendor\laravel\framework\src\Illuminate\Auth\Authenticatable.php

public function getAuthSalt()
{
return $this->salt;
}

\vendor\laravel\framework\src\Illuminate\Auth\GenericUser.php

public function getAuthSalt()
{
return $this->attributes['salt'];
}

\vendor\laravel\framework\src\Illuminate\Contracts\Auth\Authenticatable.php

public function getAuthPassword();

這樣就萬事大吉了,開心的寫代碼吧。

定義url獲取access_token

在routers.php加入下面代碼

Route::post('oauth/access_token', function() {
    return Response::json(Authorizer::issueAccessToken());
});

給需要登錄才能看到的資源(控制器)加入oauth2中間件

如routers.php加入下面代碼, 訪問購物車時需要access_token

Route::put('wechat/{uniacid}/anas_shop/cart', ['middleware' => 'oauth', 'uses' => 'Anas_shop\CartController@put']);

提交表單時需要加入

<input type="hidden" name="access_token" value="xxxxxxxxxxxx" >

獲取當前access_token的用戶id

使用Authorizer::getResourceOwnerId(); 方法即可獲取,這里寫成model供控制器調(diào)用

<?php
namespace App;  //文件路徑

use DB;
use Session;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;
use LucaDegasperi\OAuth2Server\Facades\Authorizer;

class Functions extends Model {
      protected static function memberinfo(){
        $member_id = Authorizer::getResourceOwnerId(); 獲取到的id
        $member = DB::table('ims_mc_members')->where('uid', $member_id)->first();
        return $member;
    }

}

更多oauth2的使用,去看文檔吧

希望這篇文章能幫到你!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 最近在和同學參與一個創(chuàng)業(yè)項目,用到了laravel,仔細研究了一下,發(fā)現(xiàn)laravel封裝了很多開箱即用的方法,通...
    MakingChoice閱讀 3,385評論 0 0
  • 2016-08-17 補充 Exception 部分改造方案的內(nèi)容2016-08-13 補充 View 部分改造方...
    haolisand閱讀 5,368評論 0 16
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,525評論 19 139
  • 先說幾句廢話,調(diào)和氣氛。事情的起由來自客戶需求頻繁變更,偉大的師傅決定橫刀立馬的改革使用新的框架(created ...
    wsdadan閱讀 3,190評論 0 12
  • 簡介 laravel 使實施認證的變得非常簡單,事實上,它提供了非常全面的配置項以適應應用的業(yè)務。認證的配置文件存...
    Dearmadman閱讀 6,330評論 2 13

友情鏈接更多精彩內(nèi)容