flask-jwt-extended 始终为 /login 请求提供相同的令牌
flask-jwt-extended gives same token all the time for /login requests
jwt-flask-extended 始终为任何用户发回相同的访问令牌。我已经将 Flask 与 apache 集成在一起。使用 Python 2.7.5,操作系统 - Red Hat Enterprise Linux Server release 7.3 (Maipo)。找到下面的代码。
app = Flask(__name__)
CORS(app)
@app.before_request
def log_request_info():
app.logger.debug('Headers: %s', request.headers)
app.logger.debug('Body: %s', request.get_data())
mysql = MySQL()
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'user'
app.config['MYSQL_DATABASE_PASSWORD'] = 'password'
app.config['MYSQL_DATABASE_DB'] = 'userdb'
app.config['MYSQL_DATABASE_HOST'] = 'mysql-host'
mysql.init_app(app)
# Setup the Flask-JWT-Extended extension
app.config['JWT_SECRET_KEY'] = 'Changeit' # Change this! if needed
app.config['JWT_EXPIRATION_DELTA'] = timedelta(seconds=28800)
jwt = JWTManager(app)
@app.route('/auth/token', methods=['POST'])
def login():
if not request.is_json:
return jsonify({"msg": "Missing JSON in request"}), 400
uid = request.json.get('uid', None)
username = request.json.get('username', None)
if not uid:
return jsonify({"msg": "Missing required parameter"}), 400
if not username:
return jsonify({"msg": "Missing required parameter"}), 400
# Identity can be any data that is json serializable
access_token = create_access_token(identity=uid)
return jsonify(access_token=access_token), 200
那是因为您使用的是非线程安全的全局变量。
您的访问令牌变量应该放在函数或方法中。
如果您仍然希望它可以全局访问,您可以使用 Werkzeug 的 local
包和 Flask 的 g
变量。
虽然我建议将它放在一个方法中。
WSGIPassAuthorization 开启。
我将此指令添加到 Apache mod wsgi 配置文件中。它开始按预期工作。
jwt-flask-extended 始终为任何用户发回相同的访问令牌。我已经将 Flask 与 apache 集成在一起。使用 Python 2.7.5,操作系统 - Red Hat Enterprise Linux Server release 7.3 (Maipo)。找到下面的代码。
app = Flask(__name__)
CORS(app)
@app.before_request
def log_request_info():
app.logger.debug('Headers: %s', request.headers)
app.logger.debug('Body: %s', request.get_data())
mysql = MySQL()
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'user'
app.config['MYSQL_DATABASE_PASSWORD'] = 'password'
app.config['MYSQL_DATABASE_DB'] = 'userdb'
app.config['MYSQL_DATABASE_HOST'] = 'mysql-host'
mysql.init_app(app)
# Setup the Flask-JWT-Extended extension
app.config['JWT_SECRET_KEY'] = 'Changeit' # Change this! if needed
app.config['JWT_EXPIRATION_DELTA'] = timedelta(seconds=28800)
jwt = JWTManager(app)
@app.route('/auth/token', methods=['POST'])
def login():
if not request.is_json:
return jsonify({"msg": "Missing JSON in request"}), 400
uid = request.json.get('uid', None)
username = request.json.get('username', None)
if not uid:
return jsonify({"msg": "Missing required parameter"}), 400
if not username:
return jsonify({"msg": "Missing required parameter"}), 400
# Identity can be any data that is json serializable
access_token = create_access_token(identity=uid)
return jsonify(access_token=access_token), 200
那是因为您使用的是非线程安全的全局变量。
您的访问令牌变量应该放在函数或方法中。
如果您仍然希望它可以全局访问,您可以使用 Werkzeug 的 local
包和 Flask 的 g
变量。
虽然我建议将它放在一个方法中。
WSGIPassAuthorization 开启。
我将此指令添加到 Apache mod wsgi 配置文件中。它开始按预期工作。