【发布时间】:2019-06-25 21:47:30
【问题描述】:
Laravel 5.7
PHP 7.2.10
目前我可以使用 web 和 api 保护中的任何一个,有什么方法可以同时允许两者,以便 web 应用和 api 可以一起工作。
类似
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'api|web',
'passwords' => 'users',
],
如果不使用架构,here 是一个需要更改架构的解决方案/解决方法,我不喜欢。另外我不需要访问令牌进行注册,这个答案在做什么。
api.php
Route::group([
'middleware' => 'api|web',
'prefix' => 'auth'
], function ($router) {
Route::post('register', 'Auth\AuthController@register')->name('api.register');
Route::post('forgot-password', 'Auth\ForgotPasswordController@forgotPassword')->name('api.forgot-password');
Route::post('login', 'Auth\AuthController@login')->name('api.login');
Route::middleware('auth')->post('logout', 'Auth\AuthController@logout')->name('api.logout');
web.php
Auth::routes(['verify' => true]);
Route::prefix('admin')->group(function () {
Route::middleware('auth', 'permission:super-admin|association-member')->resource('users', 'Auth\UserController');
});
配置/auth.php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web', //api
'passwords' => 'users',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
更新
正如@apokryfos 所说,If you want both to work for both then yes. However, I think that's bad practice. API routes should only allow API authentication since web authentication usually uses the session which API routes don't use anyway. If I were you I'd take a step back and rethink my entire strategy.
我也不想让两者都工作,我只想让 api 和 web 应用程序同时工作,现在我可以使用它们中的任何一个。
更新2 正如@Lim Kean Phang 建议的 git 问题链接
我变了
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth('api')->factory()->getTTL() * 60,//auth()->factory()->getTTL() * 60,
'status' => 200,
"response" => "Successfully login",
]);
}
expires_in 值,但现在我没有获得访问令牌。
api 响应是
{
"access_token": true,
"token_type": "bearer",
"expires_in": 31536000,
"status": 200,
"response": "Successfully login"
}
更新 3 添加了一个github 问题,因为找不到任何可能的解决方案来使其工作。
【问题讨论】:
-
不确定这里的用例是什么。
-
@apokryfos 好的,让我试着解释一下我的问题,如果我将使用 api 保护,我将无法通过 Web 应用程序登录,如果我将使用 web 保护非 api 端点将工作,如何同时启用?
-
您可以使用
auth:api,web作为中间件,而您通常使用auth作为中间件来让身份验证检查与两者一起使用。 -
@apokryfos,所以我需要在 web.php 和 api.php 路由中使用
auth:api,web? -
如果您希望两者都为两者工作,那么可以。但是,我认为这是不好的做法。 API 路由应该只允许 API 身份验证,因为 Web 身份验证通常使用 API 路由不使用的会话。如果我是你,我会退后一步,重新考虑我的整个策略
标签: laravel laravel-5.7 laravel-middleware laravel-guard laravel-jwt