2015-03-13 85 views
2

在web2py中,我們可以有寧靜的服務如下所述,web2py的身份驗證RESTful服務和應用程序也是用戶

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() 

這項服務將通過外部系統調用。現在如何定義兩個級別的服務使用情況。一個可以訪問服務的用戶(外部系統)。訪問後,外部系統將使用Web應用程序向最終用戶顯示相關數據。此外,將使用外部系統的最終用戶需要登錄,我將其存儲在auth相關表中。 換句話說:最終用戶使用基於外部Java的webapp進行註冊和登錄。該應用程序會將我們的web2py視爲寧靜。如果我們使用'@ auth.requires_login()'裝飾器,它是否對API調用系統或最終用戶進行身份驗證? 也有人提到,API呼叫系統可調用的

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

這意味着外部系統將在每次調用的API的web2py一次通過用戶名和密碼。即使如此,最終用戶如何登錄令牌也將被檢查/發送。

我真的很感激有人可以回答這個問題。

回答

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

相關問題