Strapi 02 :: 访问Strapi API的Authorization配置

2,357 阅读1分钟

1. 为权限管理创建用户

1.1 首先为权限管理创建用户

image.png

1.2 将新创建的用户设置为confirmed&!blocked

image.png

2. 配置你需要设置权限的API

2.1 找到api的authenticated选项

Settings->Roles(USERS & PERMISSIONS)->Authenticated image.png

2.2 将api设置为authenticated

将选中的API的Allow to perform this action for:设置为isauthenticated image.png

3. 调用apilocal/auth获取jwt

调用http://localhost:1337/auth/local的POST方法

post-body如下:

{
 "identifier": "strapi@strapi.com",
 "password": "<your password>"
}

response:

{
   "jwt": "<your jwt token>",
   "user": {
      # the properties of this user
   }
}

4. 使用jwt调用你所配置的auth-api

本文中将使用post-man来测试api的调用。

4.1 2.2中已经为Entity1的create方法设置为authenticated

4.2 使用上一篇帖子中添加Entity1的api就会返回403错误(unauthroized)

image.png

4.3 在调用api时,在authorization header中添加Bearer token

在Authorization header中加入Bearer Token就可以通过strapi的权限验证了 image.png