flask - 用于请求身份验证的带有中间件和 JWT 的 Web API(Python、Flask)引发 DecodeError('签名验证失败')
问题描述
我在解码通过请求标头收到的令牌时遇到问题。
应用:
from flask import Flask
from flask import jsonify
from flask_restplus import Resource, Api
from helpers.load import get_env as _
from middleware.environment_middleware import EnvironmentMiddleware
from flask_jwt_extended import JWTManager
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = _('DATABASE_URI')
app.config['SQLALCHEMY_DATABASE_URI'] = _('DATABASE_URI')
app.config['SECRET_KEY'] = _('SECRET_KEY')
app.config['JWT_SECRET_KEY'] = _('JWT_SECRET')
app.wsgi_app = EnvironmentMiddleware(app.wsgi_app)
jwt = JWTManager(app)
api = Api(app)
jwt._set_error_handler_callbacks(api)
中间件类:
from werkzeug.wrappers import Request, Response, ResponseStream
from helpers.load import load_db_env
from flask_jwt_extended import get_jwt_identity, jwt_required, verify_jwt_in_request
import jwt
class EnvironmentMiddleware():
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
request = Request(environ)
if request.headers:
params = load_db_env(request.headers.get('Whitelabel'))
jwt.decode(request.headers.get('Authorization').replace('Bearer ', ''), params['JWT_SECRET'], algorithm='HS256')
return self.app(environ, start_response)
res = Response(u'Unauthorized.', mimetype='application/json', status=401)
return res(environ, start_response)
load_db_env根据包括JWT_SECRET在内的“whitelabel”参数从我的数据库中带来了一个包含所有参数的字典,而我的环境带来了我需要进行身份验证的所有响应数据、标头等。
但是由于某种原因,我无法从验证和识别用户的请求中解码并找到承载令牌中的信息。
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/bela/dev/bela/lib/python3.8/site-packages/jwt/api_jwt.py", line 63, in decode
decoded = super(PyJWT, self).decode(jwt, key, verify, algorithms,
File "/home/bela/dev/bela/lib/python3.8/site-packages/jwt/api_jws.py", line 115, in decode
self._verify_signature(payload, signing_input, header, signature,
File "/home/bela/dev/bela/lib/python3.8/site-packages/jwt/api_jws.py", line 186, in _verify_signature
raise DecodeError('Signature verification failed')
jwt.exceptions.DecodeError: Signature verification failed
我希望我很清楚,我来自巴西,我的英语不是最好的。
奥布里加达!:*
解决方案
推荐阅读
- blockchain - Polkadot tx-wrapper 离线签名
- flutter - 如何动态修改文本域中每一行文本的样式?
- c++ - MPI 2D 阵列处理
- python - 使用 .set_params() 函数进行线性回归
- python - 如何将重叠卡检测为彼此独立的扭曲
- python - 太阳经度与地球的度数计算春分和冬至等
- php - 在laravel的mysql数据库中发布数组值
- sql - SQL 转换重复到一行
- android - 使用 FileProvider 或自己的 ContentProvider 提供 html 文件
- c++ - 如何检测 getline/cin 中的空格键而不忽略它或将其拆分为两个输入?