我有一個公共路線,任何用戶都可以訪問它。 (/timeline
)。
在這個動作中,如果用戶被認證,我必須告訴他他是否喜歡這個帖子。
如果路由有auth:api
中間件,我可以使用$request->user()
獲得通過身份驗證的用戶,但是如果我不使用auth:api
中間件,那麼即使用戶發送了正確的access_token,我也無法檢查用戶是否已通過身份驗證。
如何在沒有中間件的情況下檢查access_token是否正確並在控制器中驗證用戶身份?如何在沒有認證的情況下驗證用戶:laravel 5.3中的api中間件?
2
A
回答
7
您可以將警衛傳遞給您的方法,以檢查用戶是否使用特定警衛登錄。
$request->user('api');
3
您使用的是auth:api
,所以我假定您正在討論JSON請求。訪問令牌通常坐在你的請求的頭,所以你可以檢查它像這樣
public function timeline(Request $request) {
if ($request->has('access_token') || $request->header('access_token')) {
$user = Auth::guard('api')->user();
}
...
}
1
我沒有在代碼中挖的時候,但你可以看看AUTH:API中間件。你會發現身份驗證過程如何工作。如果你還沒有找到一件事讓我知道,我會在今晚看看它,並改善我的答案。
在文件Laravel\Passport\Http\Middleware\CheckClientCredentials
,你會發現這一點:
<?php
namespace Laravel\Passport\Http\Middleware;
use Closure;
use League\OAuth2\Server\ResourceServer;
use Illuminate\Auth\AuthenticationException;
use League\OAuth2\Server\Exception\OAuthServerException;
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
class CheckClientCredentials
{
/**
* The Resource Server instance.
*
* @var ResourceServer
*/
private $server;
/**
* Create a new middleware instance.
*
* @param ResourceServer $server
* @return void
*/
public function __construct(ResourceServer $server)
{
$this->server = $server;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*
* @throws \Illuminate\Auth\AuthenticationException
*/
public function handle($request, Closure $next, ...$scopes)
{
$psr = (new DiactorosFactory)->createRequest($request);
try{
$psr = $this->server->validateAuthenticatedRequest($psr);
} catch (OAuthServerException $e) {
throw new AuthenticationException;
}
foreach ($scopes as $scope) {
if (!in_array($scope,$psr->getAttribute('oauth_scopes'))) {
throw new AuthenticationException;
}
}
return $next($request);
}
}
當你越挖越深,你會看到,請求被這裏League\OAuth2\Server\RecourceServer.php
驗證。我的猜測是你會找到你的答案
+0
謝謝你Ilyas。我通過傳球后與PeterPan的幫助合作。 –
相關問題
- 1. 我如何在沒有Google Dll的情況下驗證Gmail API
- 2. 如何在沒有用戶交互的情況下驗證Google Calendar API v3?
- 3. 如何在沒有javascript的情況下驗證html文件?
- 4. Laravel的5.3認證不工作在我Laravel 5.3認證
- 5. Laravel 5.3和使用api_token的API認證
- 6. 如何驗證laravel 5.3中的數組?
- 7. 如何在laravel中驗證登錄5.3
- 8. 如何在沒有客戶端身份驗證的情況下從服務器驗證Firebase用戶?
- 9. 你可以在沒有用戶驗證的情況下使用dropbox api嗎?
- 10. Laravel中間件API的驗證
- 11. 如何在沒有SSL的情況下保護用戶的身份驗證?
- 12. 在沒有IDataErrorInfo的情況下在WinForms中添加驗證
- 13. 如何在沒有JavaScript的情況下驗證表單?
- 14. 如何在沒有Cognito的情況下驗證dynamoDB?
- 15. 如何在沒有用戶干預的情況下獲取身份驗證
- 16. Laravel 5.3 - 如何在沒有CSRF的情況下將會話添加到`API`?
- 17. laravel 5.3認證用戶提供問題
- 18. 在沒有任何身份驗證的情況下運行SSH
- 19. 如何在不驗證電子郵件或電話的情況下確認Cognito用戶池中的用戶?
- 20. Laravel 5 - 如何在沒有電子郵件確認的情況下進行身份驗證?
- 21. 如何在沒有身份驗證的情況下連接到YouTube API?
- 22. Laravel 5.3身份驗證依賴用戶
- 23. laravel 5.3驗證::用戶()找不到
- 24. 在不驗證用戶身份的情況下驗證用戶名和密碼
- 25. Rspec:如何在有效的情況下編寫條件驗證
- 26. Laravel 5.3驗證禁用
- 27. Laravel 5.3:如何在全球中間件中使用檢查身份驗證?
- 28. 如何用ajax Laravel進行驗證5.3
- 29. 如何在沒有結帳的情況下驗證svn中的軟鏈接?
- 30. Laravel 5.3中間件:創建用於身份驗證的中間件
非常感謝Peter Pan。 –
我的榮幸。 ;) – PeterPan666
您也可以在沒有請求對象的情況下使用** Auth :: guard('api') - > user()**。 – Gkiokan