如何利用laravelPassport实现API认证
内容导读
收集整理的这篇技术教程文章主要介绍了如何利用laravelPassport实现API认证,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3352字,纯文字阅读大概需要5分钟。
内容图文
这篇文章主要介绍了关于如何使用laravel Passport做API认证,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下安装larave
laravel new passport_democd passport_demo && composer install
将 .env 中数据库配置修改为自己的数据库配置
DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret
安装Passport
composer require laravel/passportphp artisan migratephp artisan passport:install
执行时可能出现以下错误
IlluminateDatabaseQueryException : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))
编辑
namespace AppProviders; use IlluminateSupportServiceProvider; use IlluminateSupportFacadesSchema;//新增 class AppServiceProvider extends ServiceProvider{ public function boot(){} public function register(){ Schema::defaultStringLength(191);//新增 } }
然后再执行这两条命令即可
php artisan migratephp artisan passport:install
编辑将添加到
namespace App; use IlluminateNotificationsNotifiable; use IlluminateFoundationAuthUser as Authenticatable; use LaravelPassportHasApiTokens;//新增 class User extends Authenticatable{ use Notifiable; use HasApiTokens;//新增 protected $fillable = ['name', 'email', 'password',]; protected $hidden = ['password', 'remember_token',]; }
在的方法中调用函数
namespace AppProviders; use IlluminateSupportFacadesGate; use IlluminateFoundationSupportProvidersAuthServiceProvider as ServiceProvider; use LaravelPassportPassport;//新增 class AuthServiceProvider extends ServiceProvider{ protected $policies = ['AppModel' => 'AppPoliciesModelPolicy',]; public function boot(){ $this->registerPolicies(); Passport::routes();//新增 } }
将配置文件中授权看守器的的选项改为
//修改前 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ],
// 修改后 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'passport', 'provider' => 'users', ], ],
创建文件,这里的代码来自另一个教程。
namespace AppHttpControllers; use IlluminateHttpRequest; use AppHttpControllersController; use AppUser; use IlluminateSupportFacadesAuth; use Validator; class UserController extends Controller { public $successStatus = 200; public function login() { if(Auth::attempt(['email' => request('email'), 'password' => request('password')])) { $user = Auth::user(); $success['token'] = $user->createToken('MyApp')->accessToken; return response()->json(['success' => $success], $this->successStatus); } else{ return response()->json(['error'=>'Unauthorised'], 401); } } public function register(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required', 'email' => 'required|email', 'password' => 'required', 'c_password' => 'required|same:password', ]); if ($validator->fails()) { return response()->json(['error'=>$validator->errors()], 401); } $input = $request->all(); $input['password'] = bcrypt($input['password']); $user = User::create($input); $success['token'] = $user->createToken('MyApp')->accessToken; $success['name'] = $user->name; return response()->json(['success'=>$success], $this->successStatus); } public function details() { $user = Auth::user(); return response()->json(['success' => $user], $this->successStatus); } }
使用postman进行测试
注册接口,注册成功后返回token与用户名
登录接口
详情接口
相关推荐:
关于Laravel之October的安装配置过程
以上就是如何利用laravel Passport实现API认证的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是为您收集整理的如何利用laravelPassport实现API认证全部内容,希望文章能够帮你解决如何利用laravelPassport实现API认证所遇到的程序开发问题。 如果觉得技术教程内容还不错,欢迎将网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。