- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在尝试在我的应用程序中捕获格式错误的 JWT 错误时遇到问题。
我正在使用 flask_jwt_extended 并且当我发送手动创建的 JWT 时。我收到此错误消息:
Error on request:
Traceback (most recent call last):
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/jwt/api_jws.py", line 180, in _load
signing_input, crypto_segment = jwt.rsplit(b'.', 1)
ValueError: not enough values to unpack (expected 2, got 1)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask_restful/__init__.py", line 266, in error_router
return self.handle_error(e)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask_restful/__init__.py", line 458, in wrapper
resp = resource(*args, **kwargs)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask_restful/__init__.py", line 573, in dispatch_request
resp = meth(*args, **kwargs)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask_jwt_extended/view_decorators.py", line 103, in wrapper
verify_jwt_in_request()
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask_jwt_extended/view_decorators.py", line 32, in verify_jwt_in_request
jwt_data = _decode_jwt_from_request(request_type='access')
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask_jwt_extended/view_decorators.py", line 267, in _decode_jwt_from_request
decoded_token = decode_token(encoded_token, csrf_token)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask_jwt_extended/utils.py", line 80, in decode_token
encoded_token, verify=False, algorithms=config.algorithm
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/jwt/api_jwt.py", line 84, in decode
payload, _, _, _ = self._load(jwt)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/jwt/api_jws.py", line 183, in _load
raise DecodeError('Not enough segments')
jwt.exceptions.DecodeError: Not enough segments
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/werkzeug/serving.py", line 302, in run_wsgi
execute(self.server.app)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/werkzeug/serving.py", line 290, in execute
application_iter = app(environ, start_response)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask_restful/__init__.py", line 269, in error_router
return original_handler(e)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask/_compat.py", line 34, in reraise
raise value.with_traceback(tb)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask_restful/__init__.py", line 269, in error_router
return original_handler(e)
File "/Users/desmondlim/.virtualenvs/rest-api/lib/python3.7/site-packages/flask/app.py", line 1719, in handle_user_exception
return handler(e)
TypeError: invalid_token() takes 0 positional arguments but 1 was given
AUTH_T wrong-token
import resource as testing
from flask import Flask, jsonify
from flask_restful import Api
from flask_jwt_extended import JWTManager
from jwt import InvalidSignatureError
app = Flask(__name__)
app.config['JWT_SECRET_KEY'] = 'secret-key'
app.config['JWT_HEADER_TYPE'] = 'AUTH_T'
app.config['JWT_BLACKLIST_ENABLED'] = True
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access', 'refresh']
app.config['PROPAGATE_EXCEPTIONS'] = True
app.config['DEBUG'] = False
api = Api(app)
jwt = JWTManager(app)
@jwt.invalid_token_loader
def invalid_token():
return jsonify({
'message': 'Invalid token.',
'error': 'invalid_token'
}), 401
@jwt.revoked_token_loader
def revoked_token():
return jsonify({
'message': 'Token is revoked.',
'error': 'revoked_token'
}), 401
@app.errorhandler(InvalidSignatureError)
def invalid_signature():
return jsonify({
'message': 'Invalid signature token.',
'error': 'wrong_token'
}), 401
api.add_resource(testing.Testing, '/test')
if __name__ == '__main__':
app.run(port=5000, debug=False)
from flask_jwt_extended import jwt_required
from flask_restful import Resource
class Testing(Resource):
@jwt_required
def get(self):
return {'message': 'okay'}, 200
最佳答案
回答我自己的问题,这样我就不会失去这些知识并帮助任何新来者。
似乎 Flask-JWT-Extended 没有处理它们的 @jwt_required 和 @jwt_refresh_token_required 装饰器的畸形标记,所以我们必须自己编写。
这些是我的装饰器代码:
def jwt_needed(func):
@wraps(func)
def decorator(*args, **kwargs):
try:
verify_jwt_in_request()
except (ValueError, DecodeError, TypeError, WrongTokenError):
return {'error': 'access token error'}, 401
return func(*args, **kwargs)
return decorator
def jwt_refresh_token_needed(func):
@wraps(func)
def decorator(*args, **kwargs):
try:
verify_jwt_refresh_token_in_request()
except (ValueError, DecodeError, TypeError, WrongTokenError):
return {'error': 'refresh token error'}, 401
return func(*args, **kwargs)
return decorator
def admin_needed(func):
@wraps(func)
def decorator(*args, **kwargs):
try:
verify_jwt_in_request()
except (ValueError, DecodeError, TypeError, WrongTokenError):
return {'error': 'access token error'}, 401
claims = get_jwt_claims()
if claims['auth'] == 'ADMIN':
return func(*args, **kwargs)
else:
return {'error': 'admin required'}, 401
return decorator
关于python-3.x - flask_jwt_extended 在解码我的 JWT 时抛出错误。我怎样才能捕捉到它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55917908/
再次尝试制作我的第一个 Flask 应用程序,这一次,(在我创建了我需要的每一个并且一切顺利之后)我试图用 flask_jwt_extended 保护一些端点,但我不能'在我的页面中找不到如何使用它们
我在尝试在我的应用程序中捕获格式错误的 JWT 错误时遇到问题。 我正在使用 flask_jwt_extended 并且当我发送手动创建的 JWT 时。我收到此错误消息: Error on reque
我想在调用@jwt_required 时向 token 添加额外的验证。我想验证其中一项声明。我可以用 JWTManager 做到这一点吗? 目前我的代码只调用: jwt = JWTManager(a
在 localhost:5000 脚本正在运行,没有任何错误。但是当涉及到符号链接(symbolic link)时,appache 日志中会出现错误。 [Sun Jun 10 17:07:16.170
我正在使用 flask 构建一个 REST API。我正在使用 postman 测试在我的数据库中创建新项目的路线,但前提是用户已登录。注册和登录的路线运行良好,最后一个使用 flask_jwt_ex
我在 Flask 应用程序中遇到了这个错误: curl http://0.0.0.0:8080/ -H "Authorization: Bearer TGazPL9rf3aIftplCYDTGDc8c
我是一名优秀的程序员,十分优秀!