Home  >  Q&A  >  body text

angular.js - 前后端分离(前后端mv*),前端如何处理授权需求(授权逻辑)呢?

前后端分离(前后端mv*)的情况下,前端如何处理授权逻辑?
(mv* 可能是mvc,也可能是mtv, 或者mvvm等等)

后端:

 BooksController
     indexAction()
     showAction($id)
     editAction($id)
         $book = Book::findOrFail($id);
         $user = Auth::user();
         //授权判断
         if ($user->hasRole/Permission('edit-book')) {
             //$acl->isAllowed($user, $book, 'edit-book')
             .....
             $book->price = Input::get('price');
             $book->save();
             ......
         }
         .......
         
     createAction()
     deleteAction($id)

后端的controller/action等操作通常使用acl/rbac等授权系统做出授权判断。

问题是:
像Emberjs、Angularjs等前端mv*框架如何处理授权部分呢?
比如未登陆(浏览用户)显式10篇文章, 登陆用户显式更多内容。

方式1)像后端一样,使用单独的授权系统/授权逻辑来处理吗?

方式2)把 授权逻辑判断结果放到json数据里面,返回给前端,前端直接使用?

BooksController
    editAction($id){
        ...
        $book = ....;
        $user = Auth::user()/Null//;登陆用户或没有登陆的匿名游客用户;
        // 授权判断, 返回1或者0
        $canEdit = $user->hasPermission('edit-book');
                   $acl->isAllowed($user, $book, 'edit-book');   
        return new JsonResponse([
            'book'=>$book,
            'user'=>$user,
            'can_edit'=>$canEdit,
            ........
        ]);
    }

3)使用Emberjs这种和后端极其相似的前端mvc框架,尤其是有完整的ember-data类orm组件的时候, 前端ember-data/model是不是要和后端简单映射一下就可以了? 安全问题的认证和授权2个部分如何处理的呢?

某草草某草草2684 days ago538

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-05-15 17:05:17

    The projects I have come into contact with use "Method 2) put the authorization logic judgment results into the json data and return it to the front end, and the front end uses it directly?" That is, the back end returns the permissions owned by the user, and then the front end makes the judgment, but I I feel that if this is the case, the security will be reduced. If the return result is tampered with, the front-end permissions will be destroyed, but this is only possible if the rule definition is known.

    reply
    0
  • Cancelreply