2010-09-14 23 views
2

在我的Pylons網站上,我有我的登錄表單將其數據發送到'https://mysite.com'。登錄成功後,將發生重定向,將其發送到他們的個人資料頁面。主塔 - 重定向將從HTTPS下降到HTTP,除非我指定協議...有沒有辦法解決這個問題?

redirect(url(controller='profile')) 

這向用戶發送到http://mysite.com/profile代替https://mysite.com/profile。我發現解決這個問題的唯一方法是改變重定向到:

redirect(url(controller='profile', protocol='https')) 

我有這樣的問題是:「如果有什麼,不管什麼原因,我的證書消失,我不得不放棄SSL」我不想通過我的整個代碼尋找我指定'https'協議的所有重定向。我想要我的登錄將用戶發送到HTTPS,這就是它...

是否有原因重定向下降到HTTP?有沒有辦法阻止它? :/

回答

0

我自定義映射,這樣每次調用「URL」將迫使正確的協議...
routing.py

class CustomMapper(Mapper): 

    def generate(self, *args, **kwargs): 
     kwargs["protocol"] = "https" 
     return Mapper.generate(self, *args, **kwargs) 

def make_map(config): 
    """Create, configure and return the routes Mapper""" 
    map = CustomMapper(directory=config['pylons.paths']['controllers'], 
         always_scan=config['debug']) 
4

因爲我花了幾個小時通過涉水掛架/路由/燒杯中的/ etc。來源我想我會分享我的解決方案。

先介紹一點情況。我在AWS上使用SSL終止的彈性負載均衡器(ELB)。該應用程序構建爲僅通過https運行;畢竟,這是一個火燒後的世界。它的分層像這樣:

ELB -> nginx -> pasteWSGI -> pylons 

ELB是簡單性方面,而是pylons.controllers.util.redirect任何呼叫快活好將觸發302重定向到的「http:// mysite的/」。 ELB在返回時不會改變(無理由),因此我的瀏覽器將被髮送回80端口,並且在該端口上沒有ELB監聽。

我試着按上面建議的更新Mapper。

  1. 沒有工作,
  2. 我想我的重定向是相對的。切換到掛機中的https意味着URL生成器會提取並提取主機以創建新的URL(https:// localhost/....)

請注意,Mapper.redirect_to可用於開箱即用相對重定向,所以沒有必要搞砸了。根本問題是controllers.redirect使用稍微不同的代碼路徑。特別是在Routes中,controllers.util.redirect不是一個重定向(有一個「if routes and routes.redirect」,它將轉化爲False)。

我的解決方案:用新的控制器方法(也稱爲重定向)替換所有調用重定向以將重定向從絕對重定向更改爲相對重定向。

代碼如下:

lib/helpers。PY

def relative_redirect(to_url, start_response): 
    """Returns a redirect that is compatible with AWS ELB (no absolute http responses) 
    Using pylons.controllers.util.redirect triggers an exception that'll be turned into a 302 
    But with an absolute path so the response does not contains https but simple http 
    """ 
    start_response("302 Found", [("Content-Type", "text/plain; charset=utf-8"), ("Location", url(to_url))]) 
    return ["You are being redirected to {0}".format(url(to_url))] 

與該位從基類我控制器稱爲:

class BaseController(WSGIController): 
    ... 
    def redirect(self, to_url): 
     """Force a relative redirection, to work with AWS ELB""" 
     return relative_redirect(to_url, self.start_response) 
相關問題