[英]Flask CORS and Flask Limiter
我正在為我的Web應用程序使用flask cors,flask limiter和AngularJS。.一切正常,但是我想要的是在前端也返回429太多請求消息,但是我似乎無法做到這一點,因為OPTIONS方法會在返回429時阻止所有內容
我的AngularJS錯誤響應代碼:
function(response){
var res_data = (response.data && response.data.data) ? response.data.data : null;
var res_status = response.status;
FlashService.Error(response[keys.issue_fields], true);
if (res_status == 513 && res_data && res_data[keys.issue_fields][0] == keys.email) {
vm.error = "Your email is not recognized. Please try again.";
} else if (res_status == 513 && res_data && res_data[keys.issue_fields][0] == keys.password){
vm.error = "Your email and password combination was incorrect. Please try again.";
} else if (res_status == 513 && res_data && res_data[keys.issue_fields][0] == keys.suspension){
vm.error = "Your account is inactive.";
}else if (res_status == 429) {
vm.error = "You have attempted a numerous login failed attempt.. Please try again later.";
}else if (res_status == -1) {
vm.error = "Server Error. Please try again later.";
}else {
vm.error = "Your email and password combination was incorrect. Please try again.";
}
vm.dataLoading = false;
});
這是我的燒瓶代碼:
@user_manager.route('/login/dp', methods=['POST'])
#make sure limit_key is changed if modify limit since it is hard coded
@limiter.limit("5/15minute")
def login_dp():
if key.email() in request.form and key.password() in request.form:
user_id = CBDPUserDatabase().login(request.form[key.email()], request.form[key.password()])
if user_id > 0:
limit_key = 'LIMITER/%s/%s/10/15/minute' % (get_ipaddr(), request.endpoint)
if limit_key in limiter._storage.storage: del limiter._storage.storage[limit_key]
if limit_key in limiter._storage.expirations: del limiter._storage.expirations[limit_key]
user_profile = CBDPUserDatabase().fetch_user_profile(user_id)
token = create_dp_token(user_id, user_profile[key.dealership()][key.id()])
if user_profile is not None:
return ResponsePacket.success(data={key.profile(): user_profile, key.token(): token})
else:
# Couldn't retrieve the user's profile
return ResponsePacket.data_exception(data={key.issue_fields(): [key.profile()]})
elif user_id == -2:
# Email address not found in user database
return ResponsePacket.data_exception(data={key.issue_fields(): [key.email()]})
elif user_id == -3:
# Password given does not match
return ResponsePacket.data_exception(data={key.issue_fields(): [key.password()]})
elif user_id == -4:
return ResponsePacket.data_exception(data={key.issue_fields(): [key.suspension()]})
else:
return ResponsePacket.request_exception()
這是我的CORS設置:
cors = CORS(application, resources={r"*": {"origins": "*"}})
這是我的燒瓶日志:
127.0.0.1 - - [19/Aug/2016 08:10:02] "OPTIONS /user/login/dp HTTP/1.1" 200 -
127.0.0.1 - - [19/Aug/2016 08:10:04] "POST /user/login/dp HTTP/1.1" 513 -
127.0.0.1 - - [19/Aug/2016 08:10:06] "POST /user/login/dp HTTP/1.1" 513 -
127.0.0.1 - - [19/Aug/2016 08:10:07] "POST /user/login/dp HTTP/1.1" 513 -
127.0.0.1 - - [19/Aug/2016 08:10:08] "OPTIONS /user/login/dp HTTP/1.1" 200 -
127.0.0.1 - - [19/Aug/2016 08:10:08] "POST /user/login/dp HTTP/1.1" 429 -
127.0.0.1 - - [19/Aug/2016 08:10:10] "POST /user/login/dp HTTP/1.1" 429 -
127.0.0.1 - - [19/Aug/2016 08:10:13] "OPTIONS /user/login/dp HTTP/1.1" 429 -
127.0.0.1 - - [19/Aug/2016 08:10:22] "OPTIONS /user/login/dp HTTP/1.1" 429 -
127.0.0.1 - - [19/Aug/2016 08:10:24] "OPTIONS /user/login/dp HTTP/1.1" 429 -
127.0.0.1 - - [19/Aug/2016 08:10:24] "OPTIONS /user/login/dp HTTP/1.1" 429 -
127.0.0.1 - - [19/Aug/2016 08:10:26] "OPTIONS /user/login/dp HTTP/1.1" 429 -
這是我的網絡控制台
我明白了! 只需放置一個方法參數
@limiter.limit("20/15minute", methods=['POST'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.