简体   繁体   English

使用laravel内置的API登录用户

[英]Logging in users with API built in laravel

I am building my first rest API for an iOS app. 我正在为iOS应用程序构建我的第一个Rest API。 The framework I use for buidling the API is Laravel. 我用来构建API的框架是Laravel。

Everything works great so far but I am not sure on how to log users in using the API. 到目前为止,一切工作都很好,但是我不确定如何使用API​​登录用户。 Could sessions work here? 会议可以在这里工作吗? Im already using SSL/HTTPS but I dont wanna authenticate users on each request, so whats the best way to only make them log in once? 我已经在使用SSL / HTTPS,但我不想在每个请求上对用户进行身份验证,那么让他们仅登录一次的最佳方法是什么?

Also, should oAuth work fine here? 另外,oAuth在这里可以正常工作吗?

If you have any examples on how to log users in on a Laravel built api please share. 如果您有任何有关如何使用Laravel内置api登录用户的示例,请分享。

Thanks in advance 提前致谢

With my experience, Laravel built in Authentication component is just be able to applied to normal authentication via form, session and cookie. 以我的经验,Laravel内置的身份验证组件仅能够通过表单,会话和cookie应用于普通身份验证。 To handled API authentication, I have used these methods, hope that one of them is suitable for you. 为了处理API身份验证,我使用了这些方法,希望其中一种适合您。

OAuth 2 OAuth 2

With the help of lucadegasperi/oauth2-server-laravel , you can make your API secured via OAuth flows. 借助lucadegasperi / oauth2-server-laravel ,您可以通过OAuth流确保API的安全。 More documentation can be found at the package wiki on Github or the PHP League Oauth2 home page . 可以在Github上的软件包WikiPHP League Oauth2主页上找到更多文档。 You can use filters to secure your API routes as follow: 您可以使用过滤器来保护API路由,如下所示:

Route::get('protected-resource', ['before' => 'oauth:scope1,scope2', function() {
    // return the protected resource
}]);

However, OAuth need a database to save client credentials and some more settings, if your API is not so complicated, this solution may not suitable. 但是,OAuth需要一个数据库来保存客户端凭据和更多设置,如果您的API并不那么复杂,则此解决方案可能不适合。

HTTP Authentication HTTP认证

This solution is more simple than OAuth and I recommend using it with an SSL (HTTPS) connection because the authentication information can be visible why using this. 该解决方案比OAuth更简单,我建议将其与SSL(HTTPS)连接一起使用,因为使用此认证的原因可以看到身份验证信息。 The packages I used before is Intervention/httpauth . 我之前使用的软件包是Intervention / httpauth You have two options with authentication method by using this package: basic (send a base64 encoded of the combination username:password via HTTP header) or digest (use MD5 algorithm to encode your information before sending via HTTP header). 使用此软件包,您可以通过两种方法使用身份验证方法: 基本 (通过HTTP标头发送username:password组合的base64编码)或摘要 (通过MD5算法对信息进行编码,然后再通过HTTP标头发送)。 This solution does not required any database. 该解决方案不需要任何数据库。

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

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