我正在使用flask-jwt-extend并尝试发送cookie。不幸的是,我没有存储在浏览器中,但我可以在标题中找到它:
Set-Cookie: refresh_token_cookie=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1OTM0NjUwNDgsIm5iZiI6MTU5MzQ2NTA0OCwianRpIjoiMjk3ZmQ2NTgtZDBmMi00ZjNlLWI0MGItNWViZDEzYzFjYWFhIiwiZXhwIjoxNTk2MDU3MDQ4LCJpZGVudGl0eSI6eyJpZCI6MSwicm9sbGUiOiJhZG1pbmlzdHJhdG9yIn0sInR5cGUiOiJyZWZyZXNoIiwiY3NyZiI6IjlmM2EzNGU2LTFjYzYtNDI4MC04NzEzLThkYmRiYTUyNzg2ZiJ9.Dcow66aWcChwvYHorP1Tk57bTT25ZP3k2Dlvuo9Vngs; Secure; HttpOnly; Path=/auth/refresh
对我来说,它看起来格式不正确:"安全"应该是"真",例如,我怀疑这就是它没有被存储的原因(这个问题正在解决同样的问题,但我没有设法用那里提供的答案来解决它(。
初始化.py(相关部分(:
from flask_jwt_extended import JWTManager
from flask_cors import CORS
from config import Config
jwt = JWTManager()
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
app.config['JWT_TOKEN_LOCATION'] = ['cookies']
app.config['JWT_COOKIE_SECURE'] = True
app.config['JWT_ACCESS_COOKIE_PATH'] = '/auth'
app.config['JWT_REFRESH_COOKIE_PATH'] = '/auth/refresh'
app.config['JWT_COOKIE_CSRF_PROTECT'] = True
app.config['JWT_COOKIE_SECURE'] = True
app.config['JWT_CSRF_CHECK_FORM'] = True
app.config['JWT_SECRET_KEY'] = "change this"
CORS(app, supports_credentials=True)
jwt.init_app(app)
routes.py(相关部分(:
@bp.route('/login', methods=['POST'])
def login():
...
resp = jsonify(access_token=access_token,
refresh_token=refresh_token)
set_access_cookies(resp, access_token)
set_refresh_cookies(resp, refresh_token)
return resp, 200
我以扩展的官方回购中的例子为灵感。我在这里做错了什么?
事实证明这是一个角度错误 - "withCredentials: true"丢失了。
login(email: string, password: string): Observable<TokenPair> {
const url: string = `${this.BASE_URL}` + '/auth/login';
return this.http.post<TokenPair>(
url,
JSON.stringify({ email, password }),
{
withCredentials: true,
},
);
}