繁体   English   中英

烧瓶CORS和烧瓶限制器

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM