dongyang5716 2014-06-13 08:59
浏览 85
已采纳

cakephp rest api和身份验证

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.

My cake project is similar to a social network and i already have programmed a big part of the web part. Now i want to start developing the API for the native mobile apps (iOS, Android, etc).

In my project i am using the standard form authentication for the normal webbrowser way.

How can i use both, basic and form authentication? Form authentication for webbrowser use and basic authentication for the native mobiel apps.

My AppController looks like:

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?

Is there a better solution? For example authenticate with tokens.

And i searched a lot about API versioning and prefix routing. The only thing i found is that cake 2.x don't support prefix routing for rest.

My goal is to have the following structure:

  • /users/view/2 for webbrowser
  • /api/1.0/users/view/2.json for the mobile apps.

In 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?

  • 写回答

1条回答 默认 最新

  • duanheye7423 2014-06-13 12:19
    关注

    Basic is stateless authentication and doesn't need a login action. 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. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况