oauth2
oauth2是一種驗證方式, 這里不加以解釋,不明白的小伙伴可以看一看阮一峰的文章-理解oauth2.0
使用的庫
https://github.com/lucadegasperi/oauth2-server-laravel
- 按github上的教程搭建oauth2認證
先進行composer安裝然后配置,數據遷移。 - 選擇一種驗證方式
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操作。 - 登錄驗證(這里使用密碼方式)
現在oauth_clients表中新建一條數據,然后每次登錄驗證時就發送參數client_id和client_secret; 在User.php修改驗證的用戶表
protected $table="ims_mc_members";
在routes.php新建以下路由
Route::post('oauth/access_token', function() {
return Response::json(Authorizer::issueAccessToken());
});
表單請求上面的路由,發送表單數據
grant_typ = "password"
client_id = "your_client_id"
client_secret = "your_client_secret"
username = "you_email@xxx.com"
password = "your_password",
注意2點,
1.按照自己寫的restapi格式發送表單,post或者get的話指定action就好,其他如delete,patch,put等就需要對應的在表單中加上一個隱藏的input如:
_method = 'delete'
2.默認用郵箱登錄
以上就搭建完了,等等,如果我登錄不用郵箱怎么辦,如果我用了salt加密怎么辦, 那往下看吧。
自定義驗證規則
有時候可能不是用郵箱登錄,或者驗證方式與上面的不同,比如我的系統使用md5+salt驗證,mobile和email都可登錄,這時候就要修改默認驗證方式了
- 修改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;
}
因為之前會正則驗證郵箱和手機號,所以就偷了個懶,用是否含有@來區分郵箱和手機號了。
-
這樣就修改成mobile和email都可登錄了
哎,上面的uniacid是什么鬼?
uniacid是因為在做微信開發,識別不同公眾號,比如權限管理,管理員和用戶都在一個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供控制器調用
<?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的使用,去看文檔吧
希望這篇文章能幫到你!