【问题标题】:Laravel Passport -> micro service user authenticationLaravel Passport -> 微服务用户认证
【发布时间】:2020-12-11 05:59:25
【问题描述】:

对于一个应用程序,我制作了一堆微服务和一个网关。网关接收请求并从 giroservices 收集数据。一切正常。

网关接受所有请求并使用 Laravel Passport 对它们进行身份验证。所以网关安装了 Laravel Passport。

[gateway]/users/login accepts the login parameters: 
[users]/users/verify login details and returns user object. All works fine. 

用户控制器.php

public function login(Request $request){

        $rules = [
            'email'     => 'required|email:rfc',
            'password'  => 'required|min:8',
        ];

        $user = $this->userService->verifyUser(['email'=> $request->email, 'password'=> $request->password]);


        return $this->successResponse(['token'=>$token], Response::HTTP_OK);
    }

$user 具有完整的用户 json,包括 UUID。我想附上那个 UUID Laravel Passports OAuth。这样当用户进行身份验证时,我可以抽象出用户 UUID 并将其用于下一个请求。

{"data":{"uuid":"94b55bed-f084-468a-a2a7-51d38e96aed3","first_name":"John","last_name":"Due","locale":"en","email":"JohnDoe@mail.com","created_at":"2020-12-10T09:40:46.000000Z","updated_at":"2020-12-10T09:40:46.000000Z"}}

显然我理解 json_decode 这个,但是如何在 Laravel Passport 中手动创建访问令牌。我该怎么做?

【问题讨论】:

    标签: php laravel oauth


    【解决方案1】:

    您可以使用密码授予 client_id 和 client_secret 以及用户凭据来创建访问令牌。您可以使用保存护照逻辑的服务中的 /oauth/token 路由来执行此操作。

    这里来自文档

    Creating A Password Grant Client
    Before your application can issue tokens via the password grant, you will need to create a password grant client. You may do this using the passport:client Artisan command with the --password option. If you have already run the passport:install command, you do not need to run this command:
    
    php artisan passport:client --password
    Requesting Tokens
    Once you have created a password grant client, you may request an access token by issuing a POST request to the /oauth/token route with the user's email address and password. Remember, this route is already registered by the Passport::routes method so there is no need to define it manually. If the request is successful, you will receive an access_token and refresh_token in the JSON response from the server:
    
    use Illuminate\Support\Facades\Http;
    
    $response = Http::asForm()->post('http://passport-app.com/oauth/token', [
        'grant_type' => 'password',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'username' => 'taylor@laravel.com',
        'password' => 'my-password',
        'scope' => '',
    ]);
    
    return $response->json();
    

    可以在这里找到。

    https://laravel.com/docs/8.x/passport#creating-a-password-grant-client

    【讨论】:

      猜你喜欢
      • 2015-09-11
      • 1970-01-01
      • 2020-12-14
      • 2019-01-03
      • 1970-01-01
      • 2019-05-27
      • 2019-01-07
      • 2015-06-21
      • 2019-07-29
      相关资源
      最近更新 更多