Python 一个人输入密钥后,瓶子密钥认证系统不起作用?

Python bottle key authentication system doesn't work after one person enters a key?

我正在尝试在我的服务器上创建一个密钥系统,只有当用户输入有效密钥作为 GET 参数时,该系统才允许用户继续访问页面。如果密钥无效或未输入密钥,他们将被重定向到如下所示的 "invalid key" 页面。

@route('/test/invalidkey')#key is invalid!
def index():
    return "Sorry, your API key is invalid! Message the admin if this is a problem."

在所有路由上,都有一个 apply=[require_key] 参数。

@route('/test/count', method='GET', apply=[require_key])

这里是require_key方法:

def require_key(fn):#check if api key is valid
    if not request.query.key:
        redirect('/test/invalidkey')
    return fn

现在没有 API 键。目前,我只是在检查 url.

中是否添加了 ?key=hey 参数

我的问题是:每当有人第一次尝试在没有 api 密钥的情况下访问页面时,它就会像预期的那样重定向到无效的密钥页面。当有人第一次使用密钥访问时,他们会按照预期的方式进行。但是......当有人访问页面后没有关键参数访问页面时已经使用关键参数访问过,他们甚至不需要关键GET参数就可以访问页面。为什么会这样?

如果只有一个人使用密钥访问它,则允许任何人不使用密钥访问它。这个关键的 GET 参数是否每次都被存储和再次引用?如果有,我该如何清除它?

我已经通读了 bottle 文档,但只有一小段文字解释了 apply 参数及其作用。这不涉及任何高级细节。

我使用这个人的博客 post 找到了解决方案。您必须使用 wayback 机器才能访问它。

http://tumblr.kurttheviking.com/post/13053496552/using-decorators-to-require-sign-in-with-bottle-py

require_key 方法应该更像这样:

def require_uid(fn):
    def check_uid(**kwargs):   
        cookie_uid = request.get_cookie('cookieName', secret='cookieSignature')

        if cookie_uid:
            //do stuff with a user object
            return fn(**kwargs)
        else:
            redirect("/loginagain")

    return check_uid