laravel/passport
在此微服务中实现了以身份验证用户。登录路由返回我使用的令牌,我用来返回tokenthis
microservice.api网关中对用户进行身份验证,并且授权将在微服务中进行。但是,如果其他微服务对用户一无所知,他们会发生授权吗? 我打算以某种方式使用有关用户角色的信息,但尚未找到如何将这些信息放入令牌
贝洛(Below)答案是一个答案基础一个通信方法,但是我强烈建议使用其他方法在微服务(例如grpc/rabbitmq/rpc/等)之间进行通信。 tops:best 原始答案 我会尝试用一个基本示例来解释。
Llet说您目前有3用户
posts核
我假设您正在使用
microservices
cookie存储用户令牌。
core中httpOnly
microservice
现在我想登录,我应该发送Route::prefix('core')->group(function () {
Route::post('register', [AuthController::class, 'register']);
Route::post('login', [AuthController::class, 'login']);
Route::middleware('scope.trader')->group(function () {
Route::get('user', [AuthController::class, 'user']);
});
});
请求,我应该想到一个解决方案
API
随时随地发送。
token
无论时间,我们都想打一个电话,我们可以简单地在此类中呼叫
UserService :
class UserService extends ApiService
{
public function __construct()
{
// Get User Endpoint Microservice API URL
$this->endpoint = env('USERS_MS') . '/api';
}
}
ApiService :
abstract class ApiService
{
protected string $endpoint;
public function request($method, $path, $data = [])
{
$response = $this->getRequest($method, $path, $data);
if ($response->ok()) {return $response->json();};
throw new HttpException($response->status(), $response->body());
}
public function getRequest($method, $path, $data = [])
{
return \Http::acceptJson()->withHeaders([
'Authorization' => 'Bearer ' . request()->cookie('token')
])->$method("{$this->endpoint}/{$path}", $data);
}
public function post($path, $data)
{
return $this->request('post', $path, $data);
}
public function get($path)
{
return $this->request('get', $path);
}
public function put($path, $data)
{
return $this->request('put', $path, $data);
}
public function delete($path)
{
return $this->request('delete', $path);
}
}
,然后我们的方法将呼叫请求,通过常见参数,并最终使用这些参数进行UserService
呼叫。
ApiService
方法,正在进行呼叫并从
API
cookie获取存储的令牌,并将其作为
Allowed methods
标头发送到目标端点,最终它将返回从目标中返回的任何内容。
API
在这里是路线:
getRequest
在控制器中:
httpOnly
这是一个完整的示例,您可以在其他微服务上使用核心方法
Authorization
方法来获取与身份验证的用户相关的信息,并且由于从class AuthController extends Controller
{
// use Services\UserService;
public UserService $userService;
/**
* @param UserService $userService
*/
public function __construct(UserService $userService)
{
$this->userService = $userService;
}
public function register(RegisterRequest $request)
{
$data = $request->only('name', 'email', 'password') + ['additional_fileds' => 0 ];
// additional fields can be used for something except from request and
// optional, like is it admin or user or etc.
// call the post method, pass the endpoint url(`register`), pass $data
$user = $this->userService->post('register', $data);
// get data from target endpoint
// and ...
return response($user, Response::HTTP_CREATED);
}
public function login(Request $request)
{
// same thing here again, but this time i passed scope to help me
// get the specific user scope
$data = $request->only('email', 'password') + ['scope' => 'writer'];
$response = $this->userService->post('login', $data);
// as you can see when user do success login, we will get token,
// which i got that token using Passport and set it to $cookie
$cookie = cookie('token', $response['token'], 60 * 24); // 1 day
// then will set a new httpOnly token on response.
return response([
'message' => 'success'
])->withCookie($cookie);
}
public function user(Request $request)
{
// Here, base on userService as you saw, we passed token in all requests
// which if token exist, we get the result, since we're expecting
// token to send back the user informations.
$user = $this->userService->get('user');
// get posts belong to authenticated user
$posts = Post::where('user_id', $user['id'])->get();
$user['posts'] = $posts;
return $user;
}
}