在實作JSON Web Tokens(JWT)時,一開始千頭萬緒不知道從何開始,後來看了web2py實作非常快速,除了驚嘆之外,忘記的也很快,所以要筆記一下,我最主要的參考來源是

https://stackoverflow.com/questions/45627766/web2py-jwt-based-authentication-refresh-token

#首先要import

from gluon.tools import AuthJWT

#user_param可以使用username email等登入時使用的

myjwt = AuthJWT(auth, secret_key=’secret’, user_param=”email”)

#使用下面這行就可以驗證取得token

def login_take_token():

myjwt.verify_expiration = False # This will allow refresh with an expired token.

#refresh_expiration_delta 預設60*60 六十分鐘需要重新認証一次

return myjwt.jwt_token_manager()

@myjwt.allows_jwt() #加上這一行就會要求有token才可以呼叫使用

@auth.requires_login()

def get_my_service(): #需要有token才可以使用的方法

    my_code

TL;DR

最後修改日期: 03/09/2021

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。