DEV Community

qiudaozhang
qiudaozhang

Posted on

masonite关于jwt的处理

config配置

config配置

注册provider

from masonite.api.providers import ApiProvider

PROVIDERS = [
...
ApiProvider
] 
Enter fullscreen mode Exit fullscreen mode

生成文件

python craft api:install 
Enter fullscreen mode Exit fullscreen mode

此时会再config目录下生成一个api.py文件。

"""API Config"""
from masonite.environment import env

from app.models.User import User

DRIVERS = {
    "jwt": {
        "algorithm": "HS512",
        "secret": env("JWT_SECRET"),
        "model": User,
        "expires": 过期时间,
        "authenticates": 认证者,
        "version": 版本,
    }
}
Enter fullscreen mode Exit fullscreen mode

就最后三项需要我们填写响应的信息

model的修改

为了支持jwt的认证,将User修改一下

__auth__ = "username"
Enter fullscreen mode Exit fullscreen mode

测试

测试认证获取jwt颁发的token

Top comments (0)