针对Restful服务以及应用程序用户的Web2py身份验证

时间:2015-03-13 18:22:29

标签: web2py restful-authentication

在web2py中,我们可以提供如下所述的宁静服务,

auth.settings.allow_basic_login = True

@auth.requires_login()
@request.restful()
def api():
   def GET(s):
       return 'access granted, you said %s' % s
   return locals()

此服务将由外部系统调用。现在如何定义两个级别的服务使用情况。一个可以访问该服务的用户(外部系统)。访问后,外部系统将使用webapp向最终用户显示相关数据。此外,将使用外部系统的最终用户需要登录,我将其存储在与auth相关的表中。 换句话说:最终用户使用基于外部java的webapp进行注册和登录。该应用程序将我们的web2py称为restful。如果我们使用'@ auth.requires_login()'装饰器,它是否对API调用系统或最终用户进行身份验证。 还有人提到api呼叫系统可以调用

curl --user name:password http://127.0.0.1:8000/myapp/default/api/hello

这意味着外部系统每次调用web2py API时都会传递用户和密码。即使这样,最终用户如何登录也将被检查/发送。

我真的很感激有人可以回答这个问题。

1 个答案:

答案 0 :(得分:2)

基本上,您需要为web2py Restfull服务进行两种身份验证。这可以使用更通用的auth.requires装饰器来实现,

@auth.requires(custom_auth, requires_login=False)

其中自定义custom_auth是模型中定义的函数,

def custom_auth():
     """Just a prototype that needs to be extended"""
     web2py_login = auth.user is not None
     other_login  = # do some other checks from a third party system
     return  web2py_login or other_login

请注意,基本身份验证为not secure,应尽可能避免在生产中使用。请改用token based authentification

相关问题