React Native で次のリクエストを送信しています。
const getData = async (cookie) => {
const resp = await fetch('/some_info');
const data = await resp.json();
console.log(data)
}
ご覧のとおり、適切なヘッダーを意図的に追加していません。
headers: {
'X-CSRF-TOKEN':value,
}
これがないと GET リクエストが失敗することを確認したかったからです。
以下は私の設定です:
JWT_ACCESS_TOKEN_EXPIRY_MINS = 15
JWT_REFRESH_TOKEN_EXPIRY_MINS = 1000
JWT_TOKEN_LOCATION = ['cookies']
JWT_COOKIE_CSRF_PROTECT = True
JWT_COOKIE_SECURE = False # change to True in prod
私のブラウザでは、次の関連する Cookie を確認できます。
エンドポイントは次のように定義されます。
@app.route('/some_info', methods=['GET'])
@jwt_required
def get_some_info():
user_identity = get_jwt_identity()
name = get_user_name_from_identity()
age = get_user_age_from_identity()
return jsonify({
'name': name,
'age': age
})
リクエストが発生すると、コンソール ログに200
が表示され、json データを確認できます。リクエスト ヘッダー (Chrome インスペクターを使用) で、 が設定されていないことがわかりX-CSRF-TOKEN
ます。なぜこれが起こっているのですか / リクエストが通過するのはなぜですか?
JWT 拡張ドキュメントから:
# By default, the CRSF cookies will be called csrf_access_token and
# csrf_refresh_token, and in protected endpoints we will look for the
# CSRF token in the 'X-CSRF-TOKEN' header. You can modify all of these
# with various app.config options. Check the options page for details.