create_access_token,并没有algorithm和private_key参数啊
时间: 2024-01-16 16:04:14 浏览: 180
Get_access_token.rar
非常抱歉,我犯了一个错误。在 Flask-JWT-Extended 中,使用非对称加密时,确实不能将算法和私钥作为参数传递给 `create_access_token()` 函数。正确的做法是,需要在 Flask-JWT-Extended 的配置中指定使用的算法和密钥。下面是修改后的代码:
```python
from flask import Flask
from flask_jwt_extended import JWTManager, jwt_required, create_access_token, get_jwt_identity
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
app = Flask(__name__)
app.config['JWT_SECRET_KEY'] = 'super-secret'
app.config['JWT_ALGORITHM'] = 'RS256' # 指定算法为 RS256
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)
public_key = private_key.public_key()
app.config['JWT_PRIVATE_KEY'] = private_key # 指定私钥
app.config['JWT_PUBLIC_KEY'] = public_key # 指定公钥
jwt = JWTManager(app)
@app.route('/login', methods=['POST'])
def login():
# 获取用户信息
username = request.json.get('username', None)
password = request.json.get('password', None)
# 验证用户信息(这里省略具体实现)
if username != 'user' or password != 'pass':
return {'msg': '用户名或密码错误'}, 401
# 生成 access token
access_token = create_access_token(identity=username)
return {'access_token': access_token}, 200
@app.route('/protected', methods=['GET'])
@jwt_required()
def protected():
# 获取当前用户身份信息
current_user = get_jwt_identity()
return {'msg': f'Hello, {current_user}!'}, 200
if __name__ == '__main__':
app.run(debug=True)
```
非常感谢您的指正,我会更加认真地检查代码的准确性。
阅读全文