简体   繁体   English

Google Appengine上的登录挂钩

[英]Login hook on Google Appengine

Every time a user logs in to the application, I want to perform a certain task, say, record the time of login. 每次用户登录应用程序时,我都要执行某项任务,比如记录登录时间。 So I wanted to know if a hook is fired on login by default? 所以我想知道默认情况下是否在登录时触发了挂钩? If yes, how can I make my module respond to it. 如果是,我该如何让我的模块响应它。

Edit - Assume there are multiple entry points in the application to login. 编辑 - 假设应用程序中有多个要登录的入口点。

While there may well be multiple points of entry, it's crucial that your auth/session code conform to the DRY principle. 虽然可能有多个入口点,但您的身份验证/会话代码符合DRY原则至关重要。

Once/if you're down to a single code path for logging in, you should be able to find an appropriate place in that code path to do something as simple as this: 一旦/如果您要使用单个代码路径进行登录,您应该能够在该代码路径中找到适当的位置来执行以下简单操作:

user.last_login = time
user.num_logins++
user.save()

Additionally, you could use a memcache cooldown to make sure this only happens once every, say, 30 minutes: 另外,你可以使用memcache冷却来确保每次只发生一次,比如30分钟:

cooldown_memcache_key = "login_cooldown_%s" % user.id
cooldown = memcache.get(cooldown_memcache_key)
if cooldown is None:
    user.last_login = time
    user.num_logins++
    user.save()
    memcache.add(cooldown_key, True, 1800)

I'm using Python on GAE (so it may be different for Java) but have seen no documentation about such a hook for a user logging in. If you used one of the session management frameworks you'd probably get some indication for that, but otherwise I do this kind of house keeping on my opening page itself which requires login. 我在GAE上使用Python(因此它可能与Java不同)但是没有看到用于登录用户的这种钩子的文档。如果你使用了一个会话管理框架,你可能会得到一些指示,但是否则我会在我的开头页面上做这种房子需要登录。 (What do you want to do about an already logged in user returning to your site a few days later... that is, do you really want to record logins or the start time of a visit/session??) (对于已经登录的用户几天后返回您的网站,你想做什么...也就是说,你真的想记录登录或访问/会话的开始时间吗?)

If I wanted to do this but with multiple landing pages, and without using a session framework, I'd use memcache to do a quick check on every page request and then only write to the datastore when a new visit starts. 如果我想这样做但有多个登陆页面,并且没有使用会话框架,我会使用memcache快速检查每个页面请求,然后只在新访问开始时写入数据存储区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM