我正在尝试在我的微服务和 API 网关之间实现用户的身份验证和授权。我现在拥有的:
laravel/passport实现此微服务中的用户身份验证。按应有的方式工作,登录路由返回令牌,我用它来验证此微服务中的用户。问题是:使用微服务进行身份验证和授权的正确方法是什么?我知道我应该在API 网关中对用户进行身份验证,并且授权将在微服务内部进行。但是,如果其他微服务对用户一无所知,如何进行授权呢?我计划以某种方式使用带有有关用户角色信息的JWT令牌,但尚未找到如何将该信息放入令牌中
我正在尝试创建一个为移动应用程序提供服务的 API。我正在将 Laravel 5.3 与 Passport 一起使用。
流程本身就是这样。移动应用程序将为 API 提供一个 facebook 访问令牌,该令牌将用于获取有关用户的一些信息(facebook id、姓名、个人资料图片和电子邮件)。届时,后端将插入(如果尚不存在)上述信息到用户表中,基本上是创建一个新用户。请注意,后端无论如何都不会登录到 Facebook,因为移动应用程序已经授予了访问令牌。
之后需要发生的是后端将向移动应用程序提供访问令牌,该令牌将用于从 API 本身访问其他路由。这应该通过 Passport 完成。据我所知,通过 Passport 需要为每个用户创建一个新客户端,但我不是在寻找它,因为移动应用程序是 API 将拥有的唯一“客户端”。通过 facebook 创建的用户实际上是移动应用程序的用户,而不是 API 本身。的架构oauth_clients是:
CREATE TABLE `oauth_clients` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`secret` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`redirect` text COLLATE utf8_unicode_ci NOT NULL,
`personal_access_client` tinyint(1) NOT NULL,
`password_client` tinyint(1) NOT NULL,
`revoked` tinyint(1) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp …Run Code Online (Sandbox Code Playgroud) 我已将这两个事件监听器添加到我的: EventServiceProvider
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'Laravel\Passport\Events\AccessTokenCreated' => [
'App\Listeners\RevokeOldTokens',
],
'Laravel\Passport\Events\RefreshTokenCreated' => [
'App\Listeners\PruneOldTokens',
],
];
Run Code Online (Sandbox Code Playgroud)
在我的AuthServiceProvider中,我有:
public function boot()
{
$this->registerPolicies();
Passport::routes();
passport::$revokeOtherTokens;
passport::$pruneRevokedTokens;
Passport::tokensExpireIn(Carbon::now()->addDays(1));
Passport::refreshTokensExpireIn(Carbon::now()->addDays(2));
}
Run Code Online (Sandbox Code Playgroud)
我希望护照撤销所有其他用户访问令牌,然后如果它们被撤销则修剪它们.但是没有任何事情发生,每当我从邮递员请求访问令牌时,我都会获得一个新的访问令牌,而数据库中有多个访问令牌.
我有 auth:api 中间件的问题!当我为非身份验证用户定义这样的路由时,我们有一个请求可供身份验证用户和非身份验证用户访问:
Route::post('{user}/leads', 'UsersController@getContact');
Run Code Online (Sandbox Code Playgroud)
当来宾用户请求此路线时,一切正常。是,我可以访问用户$request->user();
但是如果使用承载头传递令牌并获取用户$request->user()当然它不起作用!因为我们没有auth:api在这条路线上使用,如果我们这样做了,我们将无法与来宾用户一起访问这条路线!所以我找不到一种方法,我们为两个经过身份验证的用户定义一条路由,如果用户经过身份验证,我们就会得到$request->user()并且没有经过身份验证的用户也可以访问该路由!
提前致谢。
我已经在 Laravel 中应用了护照身份验证。我也在我的本地机器和 AWS 服务器上这样做了。现在我正在尝试将相同的内容应用于共享主机,在那里我将无法访问终端。所以我的好奇心只是想知道是否可以在没有的情况下申请护照php artisan passport: install?
我尝试在heroku上安装我的应用程序。该应用程序是带有“护照”进行身份验证的php-laravel应用程序。在我的本地计算机(mac os)上一切运行正常。
当我尝试对邮递员进行简单的“发布”时,出现以下错误:
2018-03-17T17:05:22.059708 + 00:00 app [web.1]:[2018年3月17日17:05:22 UTC] [2018-03-17 17:05:22]生产。错误:密钥路径“ file:///app/storage/oauth-private.key”不存在或不可读{“ exception”:“ [object](LogicException(code:0):关键路径\” file:/// app / storage / oauth-private.key \“不存在,或在/app/vendor/league/oauth2-server/src/CryptKey.php:45)处不可读”] []
要设置护照,我用生成了密钥:
PHP的工匠护照:安装
我在heroku的数据库中看到了密钥。因此该命令正常运行。
那么这是什么错误呢?
我也尝试过重新生成密钥,以停止并重新启动应用程序。没有成功。
感谢您的建议。谢谢
多米尼克
编辑:实际上,密钥文件不是在文件夹app / storage中生成的,这就是为什么出现此错误的原因。但是为什么不生成这些文件?
我正在使用 Laravel 护照,它需要在每个请求中发送要发送的标头身份验证。
是否可以将标头名称更改为 X-Access-Token?
我看到护照使用包裹
League\OAuth2\Server\AuthorizationValidators;
方法:
/**
* {@inheritdoc}
*/
public function validateAuthorization(ServerRequestInterface $request)
{
dd($request);
if ($request->hasHeader('authorization') === false) {
throw OAuthServerException::accessDenied('Missing "Authorization" header');
}
$header = $request->getHeader('authorization');
$jwt = trim(preg_replace('/^(?:\s+)?Bearer\s/', '', $header[0]));
Run Code Online (Sandbox Code Playgroud)
我试图在这里更改,但似乎标题的验证发生在此方法之前。
从最近几天开始,我开始使用Passport学习Laravel,并关注这篇精彩的文章,使用Passport使用身份验证在Laravel中创建REST API。
我创建了登录,注册,详细列出API,但是登录API中,我试图返回记录在JSON用户的详细信息,并返回所有信息,但我想只有id, name, email, phone这样如何申请select('id', 'name', 'email', 'phone')上$user = Auth::user()
我可以使用PHP取消设置功能轻松删除数据,但是我不想使用它。我只想选择需要的列,这样可以吗?
<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
class PassportController extends Controller
{
public function login()
{
if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
$user = Auth::user();
$success['token'] = $user->createToken('MyApp')->accessToken;
return response()->json(['data' => $user, 'success' => $success], 200);
} else {
return response()->json(['error' => 'Unauthorised'], 401);
}
}
}
Run Code Online (Sandbox Code Playgroud)
{
"data": { …Run Code Online (Sandbox Code Playgroud) 我正在使用Laravel 5.4和Passport4。我只想使用First-Party-App。因此,正如该答案所建议的那样,我想避免将ClientID和ClientSecret放入应用程序中。我输入了以下boot()方法AuthServiceProvider :
Passport::routes();
Passport::tokensExpireIn(Carbon::now()->addDays(30));
Passport::refreshTokensExpireIn(Carbon::now()->addDays(60));
Run Code Online (Sandbox Code Playgroud)
我添加了自己的路线api.php以接受来自App的登录:
Route::post('login', 'Auth\LoginController@apiLogin');
Run Code Online (Sandbox Code Playgroud)
这是我的动作:
public function apiLogin(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// Authentication passed...
$user = Auth::user();
$token = $user->createToken('API Access')->accessToken;
return response()->json(["token_type" =>"Bearer","expires_in" => 2592000,"access_token" => $token]);
}
return response()->json(["error" => "invalid_credentials", "message" => "The user credentials were incorrect."], 401);
}
Run Code Online (Sandbox Code Playgroud)
是否有任何方法可以检索expires_in(30天=> 2592000s)的秒数或日期时间,以便我可以自动进行计算?
我已经为我的应用程序实现了多重身份验证,它运行良好,直到它停止。我已经在互联网上搜索了解决方案,但无济于事。所以我确实有管理员登录控制器和默认的 Laravel 登录控制器,它使用 make:auth 并为用户实现可验证。我的管理员登录工作正常,但用户登录失败并返回此
BadMethodCallException Method Illuminate\Auth\RequestGuard::attempt does not exist.
Run Code Online (Sandbox Code Playgroud)
当我尝试注册用户时会发生同样的事情,但这次它返回它返回
BadMethodCallException
Method Illuminate\Auth\RequestGuard::login does not exist.
Run Code Online (Sandbox Code Playgroud)
尽管出现错误,用户注册还是通过了,并且这些字段实际上填充到了注册表中的用户表中。由于我使用的是默认身份验证,我希望它自动登录,我想这就是 attemp 方法出现问题的地方。
必须注意的是,我在另一个模块的用户模型上使用 Passport。
下面是我的登录控制器的样子
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where …Run Code Online (Sandbox Code Playgroud) laravel-passport ×10
laravel ×9
php ×6
laravel-5.3 ×2
eloquent ×1
heroku ×1
http ×1
jwt ×1
laravel-5.4 ×1
middleware ×1
oauth ×1
oauth-2.0 ×1
routes ×1