瓶子的钩子与烧杯的会话中间件和检查登录情况[英] bottle hooks with beaker session middleware and checking logins

本文是小编为大家收集整理的关于瓶子的钩子与烧杯的会话中间件和检查登录情况的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在编写带有烧杯的中间件的瓶子应用程序.

我的代码有:

@bottle.route('/')
def slash():

  try:
    beaker_session = request.environ['beaker.session']
  except:
    #redirect('/login')
    abort(401, "Failed beaker_session in slash")

  try:
    name = beaker_session['name']
  except:
    redirect('/login')

对于每个路由请求,/登录.我知道在请求之前有一个瓶钩系统要做,但是我不确定如何最好地检查某人是否已登录.

我使用瓶子是Python WebApps的新手.对于很多人来说,并没有使用Beaker Session中间件来使用它,所以我没有很多示例可以通过.

感谢您的任何帮助或指示!

ps.整个代码都在此仓库中:/labinski/blob/master/labinski.py

推荐答案

我知道在请求之前有一个瓶钩系统要做,但是我不确定如何最好地检查某人是否已登录.

您可以在每个请求之前使用before_request挂钩运行代码,但是在此处检查身份验证只有在您期望所有访问的身份验证时才有意义.您可以做这样的事情:

@bottle.hook('before_request')
def setup_request():
    try:
        beaker_session = request.environ['beaker.session']
    except:
        #redirect('/login')
        abort(401, "Failed beaker_session in slash")

    try:
        name = beaker_session['name']
    except:
        redirect('/login')

...但是如果没有一些额外的代码,当某人实际要求/login时,这将导致重定向循环.因此,您可以将其添加到钩子中,也许:

if request.urlparts.path == '/login':
    continue

另一个解决方案是使用Python Decorator实现类似的功能,该功能使您可以按方法控制访问.例如,您可以说:

@route('/')
@authenticated
def index():
    return 'This is /.'

@route('/login')
def login():
    return 'This is login.'

然后您的authenticated装饰器看起来很像钩子:

def authenticated(func):
    def wrapped(*args, **kwargs):
        try:
            beaker_session = request.environ['beaker.session']
        except:
            abort(401, "Failed beaker_session in slash")

        try:
            name = beaker_session['name']
            return func(*args, **kwargs)
        except:
            redirect('/login')

    return wrapped

本文地址:https://www.itbaoku.cn/post/1975358.html

问题描述

I'm writing a bottle application with beaker session middleware.

My code is has this:

@bottle.route('/')
def slash():

  try:
    beaker_session = request.environ['beaker.session']
  except:
    #redirect('/login')
    abort(401, "Failed beaker_session in slash")

  try:
    name = beaker_session['name']
  except:
    redirect('/login')

for each route request except /login. I know there is a bottle hook system to do things before requests, but I'm not sure how best to use it to check if someone is logged in or not.

I'm fairly new to python webapps using bottle. Not to many people are using it with beaker session middleware so I don't have a lot of examples to go by.

Thanks for any help or pointers!

PS. The entire code for this is in this repo: https://github.com/curtisgithub/labinski/blob/master/labinski.py

推荐答案

I know there is a bottle hook system to do things before requests, but I'm not sure how best to use it to check if someone is logged in or not.

You can use the before_request hook to run code prior to each request, but checking authentication here only makes sense if you expect all access to be authenticated. You could do something like this:

@bottle.hook('before_request')
def setup_request():
    try:
        beaker_session = request.environ['beaker.session']
    except:
        #redirect('/login')
        abort(401, "Failed beaker_session in slash")

    try:
        name = beaker_session['name']
    except:
        redirect('/login')

...but without some extra code this is going to result in a redirect loop when someone actually requests /login. So you can add this to the hook, maybe:

if request.urlparts.path == '/login':
    continue

Another solution is to implement similar functionality using a Python decorator, which lets you control access on a method-by-method basis. For example, you could say something like:

@route('/')
@authenticated
def index():
    return 'This is /.'

@route('/login')
def login():
    return 'This is login.'

And then your authenticated decorator would look very much like the hook:

def authenticated(func):
    def wrapped(*args, **kwargs):
        try:
            beaker_session = request.environ['beaker.session']
        except:
            abort(401, "Failed beaker_session in slash")

        try:
            name = beaker_session['name']
            return func(*args, **kwargs)
        except:
            redirect('/login')

    return wrapped