简体   繁体   English

cakephp rest api和身份验证

[英]cakephp rest api and authentication

i am developing with cakephp 2.4.7 and i am very confused and i don't know what's the best way to implement what i need. 我正在使用cakephp 2.4.7进行开发,我非常困惑,我不知道实现我所需的最佳方法是什么。

My cake project is similar to a social network and i already have programmed a big part of the web part. 我的蛋糕项目类似于社交网络,并且我已经编写了Web部分的很大一部分。 Now i want to start developing the API for the native mobile apps (iOS, Android, etc). 现在,我想开始为本地移动应用程序(iOS,Android等)开发API。

In my project i am using the standard form authentication for the normal webbrowser way. 在我的项目中,我使用标准的form authentication进行普通的Web浏览器方式。

How can i use both, basic and form authentication ? 我如何同时使用basic authenticationform authentication Form authentication for webbrowser use and basic authentication for the native mobiel apps. Web浏览器使用的表单身份验证和本机Mobiel应用程序的基本身份验证。

My AppController looks like: 我的AppController看起来像:

public $components = array(
    '...',
    'Auth' => array(
        'loginRedirect' => array(
            'controller' => 'users',
            'action' => 'index'
        ),
        'logoutRedirect' => array(
            'controller' => 'users',
            'action' => 'login'
        ),
        'authError' => 'You must be loggedin to view this page.',
        'loginError' => 'Invalid user credentials.',
        'authorize' => array('Controller'),
        'authenticate' => array(
            'Form' => array(
                'userModel' => 'User',
            )
        ),
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        )
    )
);

I know this part of the documentation: 我知道文档的这一部分:

Using multiple handlers allows you to support different ways of logging users in. When logging users in, authentication handlers are checked in the order they are declare 使用多个处理程序可以使您支持登录用户的不同方式。登录用户时,将按照声明它们的顺序检查身份验证处理程序。

But what about the login action? 但是login操作呢?

Is there a better solution? 有更好的解决方案吗? For example authenticate with tokens. 例如,使用令牌进行身份验证。

And i searched a lot about API versioning and prefix routing . 我搜索了很多有关API versioningprefix routing The only thing i found is that cake 2.x don't support prefix routing for rest. 我发现的唯一结果是,cake 2.x不支持前缀路由。

My goal is to have the following structure: 我的目标是具有以下结构:

  • /users/view/2 for webbrowser /users/view/2用于网络浏览器)
  • /api/1.0/users/view/2.json for the mobile apps. /api/1.0/users/view/2.json用于移动应用。

In UsersController: 在UsersController中:

public function view($id = null) {
// Webbrowser
}

public function api_1_0_view($id = null) {
// mobile app version 1.0
}

public function api_2_0_view($id = null) {
// mobile app version 2.0
}

Can you give me a idea how i can solve the problems? 您能给我一个我如何解决问题的想法吗?

Basic is stateless authentication and doesn't need a login action. Basic是无状态身份验证,不需要登录操作。 The credentials are passed and checked on each request. 凭据将根据每个请求进行传递和检查。 You can read here for more info. 您可以在这里阅读更多信息。

Although you can configure AuthComponent to use multiple authenticators is best not to use a stateless and stateful authentication provider together. 尽管可以将AuthComponent配置为使用多个身份验证器,但最好不要同时使用无状态和有状态的身份验证提供程序。 In your AppController's beforeFilter() you should check the url (should be easy in your case since all urls from mobile have "api" prefix) and selectively use either Form or Basic authentication provider. 在您的AppController的beforeFilter()中,您应该检查网址(在这种情况下应该很容易,因为来自移动设备的所有网址都带有“ api”前缀),并有选择地使用FormBasic身份验证提供程序。

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

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