如何使用WSGI将用户从http重定向到https [英] How to use WSGI to reroute a user from http to https

查看:1048
本文介绍了如何使用WSGI将用户从http重定向到https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b


App Engine SDK 1.6.5

Python 2.7

webapp2



我已经实现了webapp2方案来将页面保护为https。问题是,当用户说http:// site / login而不是https:// site / login时,由于这些方案没有识别路由,他们得到了404错误。



示例main.py

 #库
import webapp2

#本地控制器
from controllers.HomeHandler从controllers.LoginHandler导入HomeHandler
导入LoginHandler
$ b $ app = webapp2.WSGIApplication([
webapp2.Route(r'/',HomeHandler),
webapp2.Route(r'/ login',LoginHandler,schemes = ['https'],name ='login')
],调试= True)

我在https路由下添加了另一个路由/控制器来捕获http请求: >
webapp2.Route(r'/ login',RouteLogin)



RouteLogin.py



 #库
导入webapp2

类RouteLogin(webapp2.RequestHandler):
def get(self):
self.redirect('https://site.appspot.com/login')

这可行,但似乎应该有更好的方法来做到这一点。就像在Apache Web服务器上使用htaccess一样。这太像我喜欢的黑客了。我真的不喜欢我的代码中的硬编码网址。更不用说它是2个登录请求不是什么大问题,但可能有其他例子,最终花费太多。



注1:如果您正在查看此解决方案,请注意,使用HTTPS方案还意味着,如果不删除SCHEME或配置为dev设置的变量,您将无法使用开发控制台。



注2:我能够通过编程方式来提供HTTPS而不是HTTP。


$ b

webapp2.uri_for('login',_scheme ='https' )


这会为您提供正确的 https:// someapp。 appspot.com/login 网址。不幸的是,我不关心如何处理用户在不使用https的情况下将地址栏输入到地址栏中的主要问题,并且在我使用上面的黑客攻击时收到错误。所以我仍然在寻找WSGI的方式来将收入请求路由到HTPPS。编辑:添加了注释1并澄清了标题,我认为很明显我正在使用WSGI来自源代码而不是CGI。

解决方案

这是我在测试这个问题时使用的代码。



注意:开发Web服务器(截至写作v1.6.5)不支持https,因此您的WSGI路由将需要移除在开发环境中工作的方案。您可以在部署之前将它们添加回来,或创建一个变量来设置检查环境的方案,如下所示。



您可以通过App Engine Python重新路由请求定义app.yaml为:

app.yaml

 应用程序:cgi-vs-wsgi 
版本:1
运行时:python27
api_version:1
线程安全:是

函数库:
- 名称:webapp2
版本:latest

处理程序:

- url:/ profile
脚本:main.app
安全:总是

- url: / login
脚本:main.app
安全:总是
$ b $ - url:/.*
脚本:main.app

然后在main.py中,您可以像以下那样声明WSGI处理程序:



main.py

  import webapp2 
import os

#Models
from models.Shout进口Shout

#控制器
从controllers.HomeHandler从controllers.LoginHandler导入HomeHandler
导入LoginHandler
从controllers.ProfileHandler import ProfileHandler
$ b $如果os.environ ['SERVER_SOFTWARE']。startswith('Development'):
app_scheme ='http'
else:
app_scheme ='https'

app = webapp.WSGIApplication([
webapp2.Route(r'/ login',LoginHandler,name ='login',schemes = [app_scheme]),
webapp2.Route(r'/ profile',ProfileHandler,name ='profile',schemes = [app_scheme]),
webapp2.Route(r'/',HomeHandler)
],debug = True)

我已将此应用的代码上传到 AE-BaseApp GitHub 请随意下载并在您的应用程序中使用它。该代码是许可的Apache许可证2.0。


Original Question


App Engine SDK 1.6.5
Python 2.7
webapp2

I have implemented webapp2 schemes to secure pages to https. The issue is that when a user goes to say http:// site/login instead of https:// site/login they get a 404 error due to the schemes not recognising the route.

example main.py

#  Libraries
import webapp2

#  Local Controllers
from controllers.HomeHandler import HomeHandler
from controllers.LoginHandler import LoginHandler

app = webapp2.WSGIApplication([
    webapp2.Route(r'/', HomeHandler),
    webapp2.Route(r'/login', LoginHandler, schemes=['https'], name='login')
], debug=True)

I have added another route / controller below the https route to catch http requests:
webapp2.Route(r'/login', RouteLogin)

RouteLogin.py

#  Libraries
import webapp2

class RouteLogin(webapp2.RequestHandler):
    def get(self):
        self.redirect('https://site.appspot.com/login')

This works, but it seems there should be a better way to do this. Like using htaccess on Apache web server. This is too much like a hack for my liking. I really don't like hard coded URLs in my code. Not to mention that it is 2 requests which for login isn't a big deal, but there could be other examples where it ends up costing too much.

NOTE 1: If you are looking at this solution be aware that using the HTTPS schemes also means that you will be unable to use the dev console without deleting the SCHEME or configuring a variable that you set for dev.

NOTE 2: I was able to get a programatic way to serve HTTPS instead of HTTP. I was on the right track with the comment below but it needs an argument.

webapp2.uri_for('login', _scheme='https')
This will give you the correct https://someapp.appspot.com/login url. It unfortunately doesn't take care of my main problem of how to handle people typing the url into the address bar without https and receiving an error unless I use the hack above. So I am still looking for the WSGI way to route income requests to HTPPS.

Edits: Added Note 1 and clarified the title, I thought it was apparent that I was using WSGI from the source and not CGI.

解决方案

This is the working code I used in testing for this question.

Note: The development Web Server (as of this writing v1.6.5) doesn't support https so your WSGI routes will need the schemes removed to work in the development environment. You can add them back before deployment or create an variable to set the scheme that checks the environment as I did below.

You can get App Engine Python to reroute the request by defining app.yaml as:
app.yaml

application: cgi-vs-wsgi
version: 1
runtime: python27
api_version: 1
threadsafe: yes

libraries:
- name: webapp2
  version: latest

handlers:

- url: /profile
  script: main.app
  secure: always

- url: /login
  script: main.app
  secure: always

- url: /.*
  script: main.app

Then in main.py you can declare your WSGI Handlers as normal like:

main.py

import webapp2
import os

# Models
from models.Shout import Shout

# Controllers
from controllers.HomeHandler import HomeHandler
from controllers.LoginHandler import LoginHandler
from controllers.ProfileHandler import ProfileHandler

if os.environ['SERVER_SOFTWARE'].startswith('Development'):
    app_scheme = 'http'
else:
    app_scheme = 'https'

app = webapp.WSGIApplication([
    webapp2.Route(r'/login', LoginHandler, name='login', schemes=[app_scheme]),
    webapp2.Route(r'/profile', ProfileHandler, name='profile', schemes=[app_scheme]),
    webapp2.Route(r'/', HomeHandler)
], debug=True)

I have uploaded the code for this app to my AE-BaseApp GitHub please feel free to download and use this in your applications. The code is licensed Apache License 2.0.

这篇关于如何使用WSGI将用户从http重定向到https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆