SlideShare a Scribd company logo
1 of 51
Download to read offline
Laradebut #7
會員系統 Authentication
@author Cara Wang <caraw@cnyes.com>
@since 2017/03/28
1
PHP Developer
1. 2014/09 ~ now
2. 2015/09 ~ now: Laravel
2
會員系統要素
註冊
登入 / 登出
記得我
其他: email 驗證 / 忘記密碼 等等...
3
兩種架構
1. Server Based Authentication
4
ServerBasedAuthentication
5
ServerBasedAuthentication
6
ServerBasedAuthentication
7
ServerBasedAuthentication-其他Request需要驗證登入狀況時
8
ServerBasedAuthentication-其他Request需要驗證登入狀況時
9
ServerBasedAuthentication-其他Request需要驗證登入狀況時
10
兩種架構
1. Server Based Authentication
單靠後端操作使用者的登入狀況
使用 Session 記錄使用者登入狀況
適合用在目標客戶只有瀏覽器的產品
1. Token Based Authentication
11
TokenBasedAuthentication
12
TokenBasedAuthentication
13
TokenBasedAuthentication-其他Request需要驗證登入狀況時
14
TokenBasedAuthentication-其他Request需要驗證登入狀況時
15
兩種架構
1. Server Based Authentication
單靠後端操作使用者的登入狀況
使用 Session 與 Cookies 記錄使用者登入狀況
適合用在目標客戶只有瀏覽器的產品
1. Token Based Authentication
前後端合作操作使用者的登入狀況
需要處理 API Header 溝通使用者登入狀況
適合用在任何產品上,擴充容易
16
兩種架構 -實作會員系統要素的差異
1. 註冊: 相同
1. 登入 / 登出
Server Based Authentication: 後端利用 Session + Cookies 記錄使用者狀
態
Token Based Authentication: 前端取得 Authorization Header 告知後端使
用者狀態
17
兩種架構 -實作會員系統要素的差異
1. 記住我
Server Based Authentication: 後端利用設定 TTL 長的 Cookies 記錄使用
者狀態
Token Based Authentication: 前端定期做 Token Refresh
其他: email 驗證 / 忘記密碼 等等...: 相同
程式示範: 登入 和 記住我
18
聽起來很複雜...
Laravel 提供完整的 Server Based
Authentication
php artisan make:auth
php artisan serve
19
phpartisanmake:auth後的codechange
20
聽起來很複雜...
Laravel 提供完整的 Server Based
Authentication
php artisan make:auth
php artisan serve
php artisan migrate
21
如果你使用 Laravel 5.4 + MySQL 5.6 或以下版本
22
Schema::defaultStringLength(191);
如果你使用 Laravel 5.4 + MySQL 5.6 或以
下版本
namespace AppProviders;
use IlluminateSupportFacadesSchema;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
}
}
Ref: https://laravel-news.com/laravel-5-4-key-too-long-error
01.
02.
03.
04.
05.
06.
07.
08.
09.
23
ServerBasedAuthentication
24
ServerBasedAuthentication-Laravel
25
ServerBasedAuthentication
26
ServerBasedAuthentication-Laravel
27
ServerBasedAuthentication
28
ServerBasedAuthentication-Laravel
29
ServerBasedAuthentication
30
ServerBasedAuthentication-Laravel
31
記得我
多一個 TTL 很長的 cookies:
1|EWnGacWd8GnEUVdeeki0GLs70tjJCfb9jph2LkAbvkdTq0e8SHhA2L8mUWb0
32
ServerBasedAuthentication-RememberMe
33
ServerBasedAuthentication-RememberMe
34
Laravel 提供的 Token Based Authentication?
https://gistlog.co/JacobBennett/090369fbab0b31130b51
35
$data['token'] = str_random(20);
['token' => $data['token']]
Laravel 提供的 Token Based Authentication?
namespace AppHttpControllersAuth;
use IlluminateHttpRequest;
...
public function apiRegister(Request $request)
{
$data = $request->json()->all();
$this->validator($data)->validate();
$user = $this->create($data);
return response()->json( );
}
Stateless ?
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
36
// 1. install package
// 2. modify config/app.php
// 3. setup config
使用以下 Package:
tymondesigns/jwt-auth
composer require tymon/jwt-auth
'providers' => [
TymonJWTAuthProvidersJWTAuthServiceProvider::class,
],
'aliases' => [
'JWTAuth' => TymonJWTAuthFacadesJWTAuth::class,
],
php artisan vendor:publish --provider="TymonJWTAuthProvidersJWTAuthServiceProvider"
php artisan jwt:generate
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
37
$token = JWTAuth::attempt($data);
撰寫 登入 機制
namespace AppHttpControllersAuth;
use JWTAuth;
use IlluminateHttpRequest;
class LoginController extends Controller
{
public function apiLogin(Request $request)
{
$data = $request->json()->all();
if (!$token) {
return response()->json(['error' => 'Invalid User']);
}
return response()->json(['token' => $token]);
}
}
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
38
撰寫 登入 機制
// route/api.php
Route::post('/login', 'AuthLoginController@apiLogin');
JWT
01.
02.
39
讓 Error 的 API Response 也是 Json 格式
namespace AppExceptions;
class Handler extends ExceptionHandler
{
public function render($request, Exception $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => $exception->getMessage()], 500);
}
return parent::render($request, $exception);
}
}
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
40
TokenBasedAuthentication
41
TokenBasedAuthentication-Laravel
42
測試其他 Request
// route/api.php
use TymonJWTAuthFacadesJWTAuth;
Route::middleware('jwt')->get('/user', function (Request $request) {
return JWTAuth::parseToken()->toUser();
});
01.
02.
03.
04.
05.
43
JWTAuth::parseToken()->authenticate();
撰寫 Middleware
php artisan make:middleware JWTAuthentication
namespace AppHttpMiddleware;
use JWTAuth;
class JWTAuthentication
{
public function handle($request, Closure $next)
{
return $next($request);
}
}
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
44
撰寫 Middleware
記得綁到 Kernal 中
namespace AppHttp;
class Kernel extends HttpKernel
{
protected $routeMiddleware = [
'jwt' => AppHttpMiddlewareJWTAuthentication::class
];
}
01.
02.
03.
04.
05.
06.
07.
45
TokenBasedAuthentication
46
TokenBasedAuthentication-Laravel
47
$newToken = JWTAuth::parseToken()->refresh();
記得我
靠前端做 Token Refresh
// route/api.php
Route::get('/refresh', 'AuthLoginController@refreshToken');
public function refreshToken()
{
return response()->json(['token' => $newToken]);
}
01.
02.
01.
02.
03.
04.
05.
48
TokenBasedAuthentication-RememberMe
49
聽起來好像很簡單!?
如果都不想自己撰寫驗證程式
1. 可以考慮使用
Firebase Authentication
AWS Cognito
Auth0
50
Q & A
51

More Related Content

What's hot

ASP.NET Core 2.1設計新思維與新發展
ASP.NET  Core 2.1設計新思維與新發展ASP.NET  Core 2.1設計新思維與新發展
ASP.NET Core 2.1設計新思維與新發展江華 奚
 
advanced introduction to codeigniter
advanced introduction to codeigniteradvanced introduction to codeigniter
advanced introduction to codeigniterBo-Yi Wu
 
第一次用 PHPUnit 寫測試就上手
第一次用 PHPUnit 寫測試就上手第一次用 PHPUnit 寫測試就上手
第一次用 PHPUnit 寫測試就上手Yi-Ming Huang
 
Cas Sso Intro
Cas Sso IntroCas Sso Intro
Cas Sso IntroShiny Zhu
 
Node getting-started
Node getting-startedNode getting-started
Node getting-startedlylijincheng
 

What's hot (6)

ASP.NET Core 2.1設計新思維與新發展
ASP.NET  Core 2.1設計新思維與新發展ASP.NET  Core 2.1設計新思維與新發展
ASP.NET Core 2.1設計新思維與新發展
 
advanced introduction to codeigniter
advanced introduction to codeigniteradvanced introduction to codeigniter
advanced introduction to codeigniter
 
第一次用 PHPUnit 寫測試就上手
第一次用 PHPUnit 寫測試就上手第一次用 PHPUnit 寫測試就上手
第一次用 PHPUnit 寫測試就上手
 
Cas Sso Intro
Cas Sso IntroCas Sso Intro
Cas Sso Intro
 
Structs2簡介
Structs2簡介 Structs2簡介
Structs2簡介
 
Node getting-started
Node getting-startedNode getting-started
Node getting-started
 

Similar to Laradebut #7 - Laravel AUTH

利用OpenSSL创建并验证证书
利用OpenSSL创建并验证证书利用OpenSSL创建并验证证书
利用OpenSSL创建并验证证书Water Sky
 
Apache配置文件说明
Apache配置文件说明Apache配置文件说明
Apache配置文件说明wensheng wei
 
淘宝开放产品前端实践
淘宝开放产品前端实践淘宝开放产品前端实践
淘宝开放产品前端实践taobao.com
 
高性能远程调用解决方案
高性能远程调用解决方案高性能远程调用解决方案
高性能远程调用解决方案Ady Liu
 
透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 AppsEric ShangKuan
 
PHP & MySQL 教學
PHP & MySQL 教學PHP & MySQL 教學
PHP & MySQL 教學Bo-Yi Wu
 
Migrations 與 Schema 操作
Migrations 與 Schema 操作Migrations 與 Schema 操作
Migrations 與 Schema 操作Shengyou Fan
 
廖雪峰 Saa s ovp
廖雪峰 Saa s ovp廖雪峰 Saa s ovp
廖雪峰 Saa s ovpdrewz lin
 
Migrations 與 Schema操作
Migrations 與 Schema操作Migrations 與 Schema操作
Migrations 與 Schema操作Shengyou Fan
 
11个步骤应用Spring Security 3(西安尚学堂~付老实)
11个步骤应用Spring Security 3(西安尚学堂~付老实)11个步骤应用Spring Security 3(西安尚学堂~付老实)
11个步骤应用Spring Security 3(西安尚学堂~付老实)Underwind
 
11个步骤应用Spring Security 3
11个步骤应用Spring Security 311个步骤应用Spring Security 3
11个步骤应用Spring Security 3Underwind
 
利用Signalr打造即時通訊@Tech day geek
利用Signalr打造即時通訊@Tech day geek利用Signalr打造即時通訊@Tech day geek
利用Signalr打造即時通訊@Tech day geekJohnson Gau
 
深入研究 Windows 系統服務 效能調校與故障排除
深入研究 Windows 系統服務    效能調校與故障排除深入研究 Windows 系統服務    效能調校與故障排除
深入研究 Windows 系統服務 效能調校與故障排除5045033
 
Power flow簡介
Power flow簡介Power flow簡介
Power flow簡介Sky Wu
 
钟志 第八期Web标准化交流会
钟志 第八期Web标准化交流会钟志 第八期Web标准化交流会
钟志 第八期Web标准化交流会Zhi Zhong
 
张所勇:前端开发工具推荐
张所勇:前端开发工具推荐张所勇:前端开发工具推荐
张所勇:前端开发工具推荐zhangsuoyong
 
0502 Windwos Server 2008 Card Space 新一代身份驗證機制
0502 Windwos Server 2008 Card Space 新一代身份驗證機制0502 Windwos Server 2008 Card Space 新一代身份驗證機制
0502 Windwos Server 2008 Card Space 新一代身份驗證機制Timothy Chen
 

Similar to Laradebut #7 - Laravel AUTH (20)

使用者認證
使用者認證使用者認證
使用者認證
 
使用者認證
使用者認證使用者認證
使用者認證
 
利用OpenSSL创建并验证证书
利用OpenSSL创建并验证证书利用OpenSSL创建并验证证书
利用OpenSSL创建并验证证书
 
Apache配置文件说明
Apache配置文件说明Apache配置文件说明
Apache配置文件说明
 
淘宝开放产品前端实践
淘宝开放产品前端实践淘宝开放产品前端实践
淘宝开放产品前端实践
 
高性能远程调用解决方案
高性能远程调用解决方案高性能远程调用解决方案
高性能远程调用解决方案
 
透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps
 
PHP & MySQL 教學
PHP & MySQL 教學PHP & MySQL 教學
PHP & MySQL 教學
 
Migrations 與 Schema 操作
Migrations 與 Schema 操作Migrations 與 Schema 操作
Migrations 與 Schema 操作
 
廖雪峰 Saa s ovp
廖雪峰 Saa s ovp廖雪峰 Saa s ovp
廖雪峰 Saa s ovp
 
Migrations 與 Schema操作
Migrations 與 Schema操作Migrations 與 Schema操作
Migrations 與 Schema操作
 
11个步骤应用Spring Security 3(西安尚学堂~付老实)
11个步骤应用Spring Security 3(西安尚学堂~付老实)11个步骤应用Spring Security 3(西安尚学堂~付老实)
11个步骤应用Spring Security 3(西安尚学堂~付老实)
 
11个步骤应用Spring Security 3
11个步骤应用Spring Security 311个步骤应用Spring Security 3
11个步骤应用Spring Security 3
 
利用Signalr打造即時通訊@Tech day geek
利用Signalr打造即時通訊@Tech day geek利用Signalr打造即時通訊@Tech day geek
利用Signalr打造即時通訊@Tech day geek
 
深入研究 Windows 系統服務 效能調校與故障排除
深入研究 Windows 系統服務    效能調校與故障排除深入研究 Windows 系統服務    效能調校與故障排除
深入研究 Windows 系統服務 效能調校與故障排除
 
Power flow簡介
Power flow簡介Power flow簡介
Power flow簡介
 
驗證與訊息
驗證與訊息驗證與訊息
驗證與訊息
 
钟志 第八期Web标准化交流会
钟志 第八期Web标准化交流会钟志 第八期Web标准化交流会
钟志 第八期Web标准化交流会
 
张所勇:前端开发工具推荐
张所勇:前端开发工具推荐张所勇:前端开发工具推荐
张所勇:前端开发工具推荐
 
0502 Windwos Server 2008 Card Space 新一代身份驗證機制
0502 Windwos Server 2008 Card Space 新一代身份驗證機制0502 Windwos Server 2008 Card Space 新一代身份驗證機制
0502 Windwos Server 2008 Card Space 新一代身份驗證機制
 

More from Szuping Wang

Laradebut #5 - 關於 CRUD 外的一點小事
Laradebut #5 - 關於 CRUD 外的一點小事Laradebut #5 - 關於 CRUD 外的一點小事
Laradebut #5 - 關於 CRUD 外的一點小事Szuping Wang
 
Api survey #5 - firebase cloud messaging
Api survey #5  - firebase cloud messagingApi survey #5  - firebase cloud messaging
Api survey #5 - firebase cloud messagingSzuping Wang
 
API Survey #2 - Firebase realtime database
API Survey #2 - Firebase realtime databaseAPI Survey #2 - Firebase realtime database
API Survey #2 - Firebase realtime databaseSzuping Wang
 
cnYes 如何使用 elasticsearch
cnYes 如何使用 elasticsearchcnYes 如何使用 elasticsearch
cnYes 如何使用 elasticsearchSzuping Wang
 
cnYES 的新聞數據分析資料
cnYES 的新聞數據分析資料cnYES 的新聞數據分析資料
cnYES 的新聞數據分析資料Szuping Wang
 
team18_媒體偏坦度
team18_媒體偏坦度team18_媒體偏坦度
team18_媒體偏坦度Szuping Wang
 

More from Szuping Wang (7)

Laradebut #5 - 關於 CRUD 外的一點小事
Laradebut #5 - 關於 CRUD 外的一點小事Laradebut #5 - 關於 CRUD 外的一點小事
Laradebut #5 - 關於 CRUD 外的一點小事
 
Api survey #5 - firebase cloud messaging
Api survey #5  - firebase cloud messagingApi survey #5  - firebase cloud messaging
Api survey #5 - firebase cloud messaging
 
API Survey #2 - Firebase realtime database
API Survey #2 - Firebase realtime databaseAPI Survey #2 - Firebase realtime database
API Survey #2 - Firebase realtime database
 
cnYes 如何使用 elasticsearch
cnYes 如何使用 elasticsearchcnYes 如何使用 elasticsearch
cnYes 如何使用 elasticsearch
 
cnYES 的新聞數據分析資料
cnYES 的新聞數據分析資料cnYES 的新聞數據分析資料
cnYES 的新聞數據分析資料
 
team18_媒體偏坦度
team18_媒體偏坦度team18_媒體偏坦度
team18_媒體偏坦度
 
Git 好吃嗎
Git 好吃嗎Git 好吃嗎
Git 好吃嗎
 

Laradebut #7 - Laravel AUTH