2017-08-17 86 views
2

我正在使用Python Flask爲IBM的Bluemix編寫一個應用程序。我想強制入站請求爲https。這段代碼的作品:使用Flask的before_request強制使用HTTPS()

# We want to redirect the request to use https. X-Forwarded-Proto is only set in Bluemix runtime. 
forwarded_protocol = request.headers.get('X-Forwarded-Proto', None) 
if forwarded_protocol is not None: 
    if forwarded_protocol == 'http': 
     new_url = request.url.replace('http', 'https', 1) 
     return redirect(new_url) 

而不是把它放在我的每個路由定義,我想在一個地方做。基於Flask文檔,我認爲我想要的是使用before_request()

燒瓶文檔狀態:

的功能將被稱爲不帶任何參數。如果該函數返回非None值,則將其視爲來自視圖的返回值,並且進一步的請求處理已停止。

我認爲這意味着如果我返回None處理將繼續與請求的路由代碼。所以我的實現看起來像:

@app.before_request 
def force_https(): 
    # We want to redirect the request to use https. X-Forwarded-Proto is only set in Bluemix runtime. 
    try: 
     forwarded_protocol = request.headers.get('X-Forwarded-Proto', None) 
     if forwarded_protocol is not None: 
      if forwarded_protocol == 'http': 
       new_url = request.url.replace('http', 'https', 1) 
       return redirect(new_url) 
      else: 
       return None 
     else: 
      return None 
    except RuntimeError as e: 
     return None 

很明顯,雖然,要麼我的執行和理解是關閉。此方法運行到路由代碼後,我無法切換控制權。並且before_request()也似乎被稱爲Flask正在啓動,在第一個請求之前,因此try/except塊。我的失敗是否與我的執行有關,對Flask的理解是什麼?

+0

簽出此擴展程序:https://github.com/kennethreitz/flask-sslify – AArias

回答

1

您可以檢查請求端點是否是視圖函數之一以避免運行時錯誤。 return None在技術上等同於return,但是如果你什麼都不做,python中的函數會自動返回None。請確保將「http://」替換爲「https://」而不僅僅是「http」,因爲字符串「http」可能發生在URL中的任何其他位置。並且request.is_secure也許是檢查請求是否比檢查X-Forwarded-Proto標頭更安全的更好方法。請嘗試以下方法:

@app.before_request 
def force_https(): 
    if request.endpoint in app.view_functions and not request.is_secure: 
     return redirect(request.url.replace('http://', 'https://')) 

您也可以創建自己的裝飾器來重定向非安全請求。例如,請參閱此片段:http://flask.pocoo.org/snippets/93/

+0

感謝@darksky,這讓我走上了正確的道路。對您的建議進行一處修改,不確定這是否適用於Bluemix。協議總是以http的形式發送到我的應用程序。原始協議位於「X-Forwarded-Proto」標題中。 'request.is_secure'永遠是假的。對我有用的條件是'if app.view_functions和request.headers.get('X-Forwarded-Proto',None)中的request.endpoint =='http':' – user2085050