如何在Flask-JWT中设置自定义授权响应? [英] How to set a custom authorization response in Flask-JWT?
问题描述
Flask-JWT本身可以工作,但是我需要在http响应中添加一些值,我尝试这样做:
By itself, Flask-JWT works, but I need to add some values to the http response, I try to do it like this:
@jwt.auth_response_callback
def custom_auth_response_handler(access_token, identity):
response = Response({
'accessToken': access_token.decode('utf-8')
})
response.headers['Access-Control-Allow-Origin'] = '*'
return response
我添加此代码后,在启动Flask时收到错误消息:
As soon as I add this code I get an error when starting Flask:
Traceback (most recent call last):
File "wsgi.py", line 1, in <module>
from app import create_app
File "D:\Dev\api-general\app\__init__.py", line 40, in <module>
def customized_response_handler(access_token, identity):
TypeError: _default_auth_response_handler() missing 1 required positional argument: 'identity'
正如我所说,没有此代码,一切都可以工作.看起来该函数应该为装饰器返回一些内容,但是除了HTTP响应之外,我还应该返回什么?
As I said, everything works without this code. It looks like the function should return something for the decorator, but what should I return besides the HTTP response?
P.S.我尝试从函数中删除 identity
参数-它完全不会改变任何内容.
P.S. I tried removing the identity
parameter from the function - it doesn't change anything at all.
推荐答案
该库已经有5年不受支持了,因此我使用了 Flask-JWT-Extended
.可能可以解决原始问题,但是我认为这没有道理.
The library has not been supported for 5 years, so I used Flask-JWT-Extended
. There may be a solution to the original problem, but I don't think that makes sense.
这篇关于如何在Flask-JWT中设置自定义授权响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!