大家好!
我正在建立一個網站,但我一直在發送表格。我的問題是它發送access_token_cookie,但觸發unauthorized_token_callback. 我看到了請求和回應,請求包含令牌,但代碼假裝它甚至不存在。
我無法理解它,我需要一些好的提示。
這是 JWT 配置,此代碼段中不包含密鑰:
application.config['JWT_TOKEN_LOCATION'] = ['cookies', 'headers', 'json', 'query_string']
application.config['JWT_COOKIE_SECURE'] = False #! Needs to be changed in production
application.config['JWT_COOKIE_CSRF_PROTECT'] = True
application.config['JWT_CSRF_CHECK_FORM'] = True
application.config['JWT_COOKIE_SAMESITE'] = 'Strict'
application.config['CSRF_COOKIE_NAME'] = 'csrf_token'
application.config['JWT_ACCESS_CSRF_HEADER_NAME'] = ['X-CSRF-TOKEN-ACCESS', 'X-CSRFToken']
application.config['JWT_CSRF_IN_COOKIES'] = True
我正在使用自定義裝飾器,它根據用戶的角色限制用戶訪問:
def Xrole_required(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
verify_jwt_in_request()
role = dth.get_user_by_email(get_jwt_identity()).roles.role_name
if(role != 'required_role'):
flash('Xrole required', 'error')
return Response(render_template('404.html', title='Not Logged In'), 404, mimetype='text/html')
else:
return fn(*args, **kwargs)
return wrapper
每當我洗掉裝飾器時,它都會毫無問題地重定向我,但使用它表示我沒有登錄。我在其他地方使用這個裝飾器,它作業得很好。
有趣的是,這適用于每個GET請求,但不適用于其他方法。這是表格,所以你可以看到它:
<form class="form-control" action="{{ url_for('MyEndpoint_name', show='active') }}" method="POST">
<div class="row pt-2">
<label for="desc" class="required">Description</label>
<input id="desc" name="desc" type="text" required class="shadow-sm rounded border">
</div>
<div class="row pt-2">
<label>Text</label>
<input type="text" class="shadow-sm rounded border">
</div>
<div class="row pt-2">
<label>Number</label>
<input type='number' value="0" min="0" class="shadow-sm rounded border">
</div>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
這是處理請求的資源:
class MyResource(Resource):
@Xrole_required
def post(self, show):
#here comes the code, this is only for test
return redirect(url_for('/redirect_page'), 303)
我是在這里某個地方犯了錯誤,還是我錯過了檔案的某些關鍵部分?
請注意,我不想使用XMLHttpRequest,因為這將是一個矯枉過正,我已經有很多 js 并且這些都是簡單的形式。
uj5u.com熱心網友回復:
您可能遇到了 CSRF 問題。確保將 JWT_CSRF_CHECK_FORM 設定為 True 并使用https://flask-jwt-extended.readthedocs.io/en/stable/api/#flask_jwt_extended.get_csrf_token在表單的隱藏欄位中包含 CRSF 令牌
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/467915.html
上一篇:如何將python燒瓶應用程式中的windows檔案夾與sqlite資料庫表同步?
下一篇:阻止專案添加到字典
