Thinkphp5 – using think API to deploy JWT

Time:2021-1-15

Because the next project will use TP5 to develop a small program, so we use the TP framework. Because small program development needs the background to write API interface, we check whether there are related dependencies on the Internet. Here we recommend the think API extension tool, because we mainly want to use the JWT function to judge the login status of small program users. Let’s take my project as an example For example, let’s talk about how to deploy JWT through think API.

1. Installation dependency

The TP version I use is tp5.1, and the extension download address is:https://github.com/czewail/think-api, bycomposerInstallation dependency:

$ composer require zewail/think-api:1.1.x

2. Configuration description

After the extension is installed, we canvendor/think-api/config/jwt.phpFile to see the configuration of JWT.

It is mainly the user model path, which needs to be modified:

return [

3. Create API interface controller

Create a token from the command line that we want to return to the front controller

$ php think make:controller api/Index

4. Reference dependency

Add a file path to the created file header:use Zewail\Api\Facades\JWT;The case code is as follows:

public function index()

One thing to note is that the API’s default receive parameters are mobile and password. For example, Tel and password. The variable name has been changed, so we need to insert the following code into the user model to explain. and so on.

public $jwtSub = 'tel';

5. Configure routing

stayroute/route.phpAdd routing address toRoute::get('api/test', 'api/Index/index');

6. Operation test

In postman, the test results are as follows, so that we can generate the token in the background and return it to the foreground to verify the login.

Thinkphp5 - using think API to deploy JWT

7. Verification token (Supplement)

In postman, we send the previously generated token to the background in the form of header to verify the token. The following figure is an example.

Thinkphp5 - using think API to deploy JWT

Then carry the header to access and verify the route. The verification code is as follows:

if ($user = JWT::authenticate()) {
                return true;
        }

True is returned if the validation is correct.

8. How to deal with the problem that the token does not exist and the token is expired (Supplement)

stayvendor\think-api\src\JWT/Factories\code.phpThe think API interface in the file provides us with corresponding error feedback.

//Check for expiration

So how can we use the state feedback? We need to use the front-end middleware to verify the token information sent by the front-end.

First, create the middleware

$ php think make:middle Test

Then write the following in the middleware:

//Capturing error feedback with try catch

After that, you can refer to the route.

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Oracle automatic collection task statistics, SQL tuning

–View auto collection task status SELECT CLIENT_NAME,STATUS FROM DBA_AUTOTASK_CLIENT; SELECT CLIENT_NAME, STATUS FROM DBA_AUTOTASK_CLIENT WHERE CLIENT_NAME = ‘auto optimizer stats collection’; –Turn off auto optimizer stats collection BEGIN DBMS_AUTO_TASK_ADMIN.DISABLE(CLIENT_NAME => ‘auto optimizer stats collection’, OPERATION => NULL, WINDOW_NAME => NULL); END; / –Shut down SQL optimizer SQL Tuning Advisor (STA) BEGIN DBMS_AUTO_TASK_ADMIN.DISABLE(CLIENT_NAME => ‘sql tuning […]