Amy*_*yth 4 authentication google-app-engine redirect oauth-2.0
我在我的GAE应用程序中使用Alex的SimpleAuth(https://github.com/crhym3/simpleauth).我的基本模板中有一个Jquery Powered登录框,这意味着用户可以从应用程序内的任何URL登录.我希望将用户重定向回他们要求登录的页面.我们有什么办法可以用Oauth2实现这个目的,还是我们可以只将用户重定向回一个特定的网址?
如果您正在使用SimpleAuth,我假设您可能使用webapp2,因此@jmort253示例并不完全是我要做的(例如,webapp2具有内置会话,因此为什么还要使用另一个库进行会话处理).
虽然,从概念上讲它是正确的:您需要的是在开始身份验证过程之前在会话中的某个位置存储原始URL.然后在成功验证后使用该存储的URL进行最终重定向.
从SimpleAuth的示例应用程序代码开始,您基本上需要更改的是_on_signin()的最后一行,以将用户重定向到他们来自的原始URL(而不是'/ profile').
要存储原始请求URL,您可以使用简单的包装器,例如
def simpleauth_login_required(handler_method):
"""A decorator to require that a user be logged in to access a handler.
To use it, decorate your get() method like this:
@simpleauth_login_required
def get(self):
user = self.current_user
self.response.out.write('Hello, ' + user.name())
"""
def check_login(self, *args, **kwargs):
if self.request.method != 'GET':
self.abort(400, detail='The login_required decorator '
'can only be used for GET requests.')
if self.logged_in:
handler_method(self, *args, **kwargs)
else:
self.session['original_url'] = self.request.url
self.redirect('/my-login-page-where-users-can-choose-auth-method')
return check_login
Run Code Online (Sandbox Code Playgroud)
现在,回到_on_signin()重定向行,而不是self.redirect('/profile')你做这样的事情:
target = self.session['original_url']
self.redirect(target)
Run Code Online (Sandbox Code Playgroud)
几个笔记:
logged_in方法,指示当前请求是否由已经过身份验证的用户发出;以上示例的信用转到webapp2_extras.appengine.users模块.
| 归档时间: |
|
| 查看次数: |
1234 次 |
| 最近记录: |