简体   繁体   English

Angular Auth反对Laravel后端

[英]Angular Auth against Laravel backend

I am creating an app using Laravel and building a small internal API to connect to with an Angular frontend. 我正在使用Laravel创建一个应用程序并构建一个小的内部API来连接到Angular前端。

I have the auth working, but wanted to ensure that this is an acceptable way to log in a user, and to make sure everything is secure. 我有auth工作,但希望确保这是一种可接受的方式来登录用户,并确保一切都是安全的。

Sessions Controller: 会话控制器:

public function index() {
    return Response::json(Auth::check());
}


public function create() {
    if (Auth::check()) {
        return Redirect::to('/admin');  
    }
    return Redirect::to('/');
}

public function login() {

    if (Auth::attempt(array('email' => Input::json('email'), 'password' => Input::json('password')))) {
        return Response::json(Auth::user());
        // return Redirect::to('/admin');
    } else {
        return Response::json(array('flash' => 'Invalid username or password'), 500);   
    }       
}

public function logout() {
    Auth::logout();
    return Response::json(array('flash' => 'Logged Out!'));
}

Laravel Route: Laravel路线:

Route::get('auth/status', 'SessionsController@index');

Angular Factory: 角厂:

app.factory('Auth', [ "$http", function($http){


var Auth = {};

Auth.getAuthStatus = function() {
    $http({
        method: "GET", 
        url: "/auth/status", 
        headers: {"Content-Type": "application/json"}
    }).success(function(data) {
        if(!data) {
        console.log('Unable to verify auth session');
        } else if (data) {
            console.log('successfully getting auth status');
            console.log(data);              

            // return $scope.categories;
            Auth.status = data;
            return Auth.status;
        }
    });

}

return Auth;

}

]);

I would then essentially wrap the whole app in something like an "appController" and declare the 'Auth' factory as a dependency. 然后我会将整个应用程序包装成类似“appController”的东西,并将'Auth'工厂声明为依赖项。 Then I can call Auth.getAuthStatus() and hide / show things based on the user state since this will essentially be SPA. 然后我可以调用Auth.getAuthStatus()并根据用户状态隐藏/显示内容,因为这基本上是SPA。

I realize I also need to hide the /auth/status URI from being viewed / hit by anyone, and was wondering how to do that as well. 我意识到我还需要隐藏/ auth / status URI不被任何人查看/命中,并且想知道如何做到这一点。 Kind of a general question but any insight would be greatly appreciated. 这是一个普遍的问题,但任何见解将不胜感激。 Thanks. 谢谢。

Great question. 好问题。 I've answered this same question before so I will say the same thing. 我之前已经回答了同样的问题,所以我会说同样的话。

Authentication is a bit different in SPAs because you separate your Laravel app and Angular almost completely. 在SPA中进行身份验证有点不同,因为您几乎完全分离了Laravel应用程序和Angular。 Laravel takes care of validation, logic, data, etc. Laravel负责验证,逻辑,数据等。

I highly suggest you read the article linked at the bottom. 我强烈建议你阅读底部链接的文章。

You can use Laravel's route filters to protect your routes from unauthorized users. 您可以使用Laravel的路由过滤器来保护您的路由免受未经授权的用户的攻击。 However, since your Laravel application has now become an endpoint only, the frontend framework will be doing the heavy lifting as far as authentication and authorization. 但是,由于您的Laravel应用程序现在只是一个端点,因此前端框架将在认证和授权方面做得非常繁重。

Once you have route filters set, that doesn't prevent authorized users from attempting to do actions that they are not authorized to do. 设置路由过滤器后,这不会阻止授权用户尝试执行他们无权执行的操作。

What I mean by the above is for example: 我的意思是上面的例子:

You have an API endpoint: /api/v1/users/159/edit 您有一个API端点:/ api / v1 / users / 159 / edit

The endpoint is one of the RESTful 7, and can be used to edit a user. 端点是RESTful 7之一,可用于编辑用户。 Any software engineer or developer knows that this is a RESTful endpoint, and if authorized by your application, could send a request with data to that endpoint. 任何软件工程师或开发人员都知道这是一个RESTful端点,如果您的应用程序授权,可以向该端点发送包含数据的请求。

You only want the user 159 to be able to do this action, or administrators. 您只希望用户159能够执行此操作或管理员。

A solution to this is roles/groups/permissions whatever you want to call them. 无论您想要调用它们,解决方案都是角色/组/权限。 Set the user's permissions for your application in your Angular application and perhaps store that data in the issued token. 在Angular应用程序中设置应用程序的用户权限,并可能将该数据存储在已颁发的令牌中。

Read this great article (in AngularJS) on how to authenticate/authorize properly using frontend JavaScript frameworks. 阅读这篇伟大的文章(在AngularJS中),了解如何使用前端JavaScript框架正确地进行身份验证/授权。

Article: https://medium.com/opinionated-angularjs/techniques-for-authentication-in-angularjs-applications-7bbf0346acec 文章: https//medium.com/opinionated-angularjs/techniques-for-authentication-in-angularjs-applications-7bbf0346acec

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

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