blog-laravel-jwt's People
blog-laravel-jwt's Issues
JWT官網的問題
https://jwt-auth.readthedocs.io/en/develop/quick-start/
* Get the identifier that will be stored in the subject claim of the JWT.
* @return mixed
public function getJWTIdentifier()
{
return $this->getKey();
}
* Return a key value array, containing any custom claims to be added to the JWT.
* @return array
public function getJWTCustomClaims()
{
return [];
}
第一個是認證在這個專案(物件)中宣告JWT
第二個是返回key數值的陣列,包含任何需要宣告的加入這個JWT
國字是這樣,但功用以及含意不懂
https://jwt-auth.readthedocs.io/en/develop/auth-guard/
// Generate a token for the user if the credentials are valid
$token = auth()->attempt($credentials);
// Get the token
$token = auth()->login($user);
attempt以及login有什麼差別,
國字翻譯是嘗試以及登入,
但實際差別不清楚。
JWT的了解疑問
https://mgleon08.github.io/blog/2018/07/16/jwt/
https://yami.io/jwt/
你能夠直接在 JWT 中存放資料,
而不用額外呼叫資料庫。
這句話完全看不懂,
我理解是把jwt的資訊存入資料庫內做Token的認證,
如果比對資訊不從資料庫來,
總不可能是中間伺服器發送那邊傳過來,
然後做對照吧!
微服務架構下,
我們可能將服務分散在不同的伺服器上,
JWT的token只要完成一次驗證,
取得認證令牌後,
就可以讓使用者在不同的伺服器上存取資源,
卻不需要不斷做身份驗證。
為何可以不了解原因。
我了解狀態性是有兩個伺服器,其中一個有記憶Token,另一個卻無此Token數據,需要重新註冊,
JWT分成三個section,分別是header、payload與signature:
第一個是認證的方式,裡面有加密的類型
第二個是本身要紀錄資料,
前兩個是有Base64加密過,只是此加密非安全加密(可逆推數據回去),
最後一個則是有另外加密後的識別資訊(透過secret的一個string加密)。
token是一個有時效性的通行證,在登入後的這段時間不需重複做登入的行為。
cookie是在自己電腦上存明碼認知的資料(相同的資料內容)
session是在伺服端存加密後的資訊,讓客戶端存一組可以認證的加密亂碼,
JWT是指中間還令另有一個伺服端,
給予主伺服端以及客戶端共同的加密認證嗎?
因為可以在不同的主伺服端進行無須登入的權限控制,
加密的過程知道是怎樣的,
但不了解原來是如何,或是哪裡的資料做認證。
token是token的改善方式嗎?
兩項有何差異,物件以及此物件內的欄位
User::create是物件的資料型態
$request->name
$request要物件的資料型態
$createUser = User::create([
'name' => $request->name,
]);
$data['name']要string的資料型態
$data要陣列的資料型態
return User::create([
'name' => $data['name'],
]);
JWT refresh的疑問
public function handle($request, Closure $next)
{
// 检查此次请求中是否带有 token,如果没有则抛出异常。
$this->checkForToken($request);
// 使用 try 包裹,以捕捉 token 过期所抛出的 TokenExpiredException 异常
try {
// 检测用户的登录状态,如果正常则通过
if ($this->auth->parseToken()->authenticate()) {
return $next($request);
}
throw new UnauthorizedHttpException('jwt-auth', '未登录');
} catch (TokenExpiredException $exception) {
// 此处捕获到了 token 过期所抛出的 TokenExpiredException 异常,我们在这里需要做的是刷新该用户的 token 并将它添加到响应头中
try {
// 刷新用户的 token
$token = $this->auth->refresh();
// 使用一次性登录以保证此次请求的成功
Auth::guard('api')->onceUsingId($this->auth->manager()->getPayloadFactory()->buildClaimsCollection()->toPlainArray()['sub']);
} catch (JWTException $exception) {
// 如果捕获到此异常,即代表 refresh 也过期了,用户无法刷新令牌,需要重新登录。
throw new UnauthorizedHttpException('jwt-auth', $exception->getMessage());
}
}
https://segmentfault.com/a/1190000013151506
解决使用jwt刷新token带来的问题
} catch (TokenExpiredException $e) {
try {
sleep(rand(1,5)/100);
$newToken = JWTAuth::refresh($token);
$request->headers->set('Authorization','Bearer '.$newToken); // 给当前的请求设置性的token,以备在本次请求中需要调用用户信息
// 将旧token存储在redis中,30秒内再次请求是有效的
Redis::setex('token_blacklist:'.$token,30,$newToken);
} catch (JWTException $e) {
// 在黑名单的有效期,放行
if($newToken = Redis::get('token_blacklist:'.$token)){
$request->headers->set('Authorization','Bearer '.$newToken); // 给当前的请求设置性的token,以备在本次请求中需要调用用户信息
return $next($request);
}
// 过期用户
return response()->json([
'code' => '2',
'msg' => '账号信息过期了,请重新登录',
]);
}
https://blog.csdn.net/weixin_34221073/article/details/88730824
laravel 使用jwt一些坑,token过期?刷新返回前端?问题多多!
public function tokenValidator(&$request,$jwt){
#检测request中header头是否带了token
if(is_null($token = \request() ->header('authorization'))){
$this->response(400,'Authorization Failed,未携带Authorization');
}
#提取token中用户数据
try{
$user = $jwt->parseToken()->toUser()->toArray();
if(! $user){
$this->response(200,'用户不存在','');
}
}catch (TokenExpiredException $exception){
#异常处理 token过有效期,进行刷新
try{
$token = $jwt->refresh();
$access_token = 'Bearer'.$token;
$request->headers->set('Authorization',$access_token);
}catch(JWTException $exception){
#refresh 也过期,重新登录
$this->response(400,'Authorization过期,重新登录');
}
}
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.