使用 jwt_required 添加资源?
Adding resources with jwt_required?
我使用 Flask 创建了一个 API,其中使用 flask_jwt_extended 身份验证工作正常。
但是,如果我添加具有 jwt_required 装饰器的资源,则会出现此错误。
File "/Library/Python/2.7/site-packages/flask_jwt/__init__.py", line 176, in decorator
_jwt_required(realm or current_app.config['JWT_DEFAULT_REALM'])
KeyError: 'JWT_DEFAULT_REALM'
示例资源:
class Endpoint(Resource):
@jwt_required()
def get(self):
return {"State": "Success"}
正在初始化应用程序:
app = Flask(__name__)
api = Api(app)
正在添加资源:
api.add_resource(resource_class, "/myEndpoint")
我让它工作的唯一方法是在与 API 相同的文件中定义端点 class。
我想我需要以某种方式将 Realm 传递到端点 class 并使用 jwt_required 上的可选参数来设置 Realm。
我想你忘了初始化 JWT 实例。你可以通过两种方式做到这一点。 第一个:
from flask import Flask
from flask_jwt import jwt_required, JWT
from flask_restful import Resource, Api
class Endpoint(Resource):
@jwt_required()
def get(self):
return {"State": "Success"}
app = Flask(__name__)
app.config['SECRET_KEY'] = 'super-secret'
def authenticate(username, password):
# you should find user in db here
# you can see example in docs
user = None
if user:
# do something
return user
def identity(payload):
# custom processing. the same as authenticate. see example in docs
user_id = payload['identity']
return None
# here what you need
jwt = JWT(app, authenticate, identity)
api = Api(app)
api.add_resource(Endpoint, '/myEndpoint')
if __name__ == '__main__':
app.run(debug=True)
app.run(host='0.0.0.0')
第二种方式是更新我们的应用配置。只需更改:
jwt = JWT(app, authenticate, identity)
收件人:
app.config.update(
JWT=JWT(app, authenticate, identity)
)
开始我们的路线吧。你会看到:
{
"description": "Request does not contain an access token",
"error": "Authorization Required",
"status_code": 401
}
希望对您有所帮助。
发现问题,在我导入的资源中 jwt_required
:
from flask_jwt_extended import jwt_required
但是我需要从初始化 JWT 的 class import jwt_required
。
我使用 Flask 创建了一个 API,其中使用 flask_jwt_extended 身份验证工作正常。
但是,如果我添加具有 jwt_required 装饰器的资源,则会出现此错误。
File "/Library/Python/2.7/site-packages/flask_jwt/__init__.py", line 176, in decorator
_jwt_required(realm or current_app.config['JWT_DEFAULT_REALM'])
KeyError: 'JWT_DEFAULT_REALM'
示例资源:
class Endpoint(Resource):
@jwt_required()
def get(self):
return {"State": "Success"}
正在初始化应用程序:
app = Flask(__name__)
api = Api(app)
正在添加资源:
api.add_resource(resource_class, "/myEndpoint")
我让它工作的唯一方法是在与 API 相同的文件中定义端点 class。
我想我需要以某种方式将 Realm 传递到端点 class 并使用 jwt_required 上的可选参数来设置 Realm。
我想你忘了初始化 JWT 实例。你可以通过两种方式做到这一点。 第一个:
from flask import Flask
from flask_jwt import jwt_required, JWT
from flask_restful import Resource, Api
class Endpoint(Resource):
@jwt_required()
def get(self):
return {"State": "Success"}
app = Flask(__name__)
app.config['SECRET_KEY'] = 'super-secret'
def authenticate(username, password):
# you should find user in db here
# you can see example in docs
user = None
if user:
# do something
return user
def identity(payload):
# custom processing. the same as authenticate. see example in docs
user_id = payload['identity']
return None
# here what you need
jwt = JWT(app, authenticate, identity)
api = Api(app)
api.add_resource(Endpoint, '/myEndpoint')
if __name__ == '__main__':
app.run(debug=True)
app.run(host='0.0.0.0')
第二种方式是更新我们的应用配置。只需更改:
jwt = JWT(app, authenticate, identity)
收件人:
app.config.update(
JWT=JWT(app, authenticate, identity)
)
开始我们的路线吧。你会看到:
{
"description": "Request does not contain an access token",
"error": "Authorization Required",
"status_code": 401
}
希望对您有所帮助。
发现问题,在我导入的资源中 jwt_required
:
from flask_jwt_extended import jwt_required
但是我需要从初始化 JWT 的 class import jwt_required
。