如何在 laravel 中同时启用 api 和网络防护
How to enable both api and web guard in laravel
Laravel 5.7
PHP 7.2.10
目前我可以使用 web 和 api guards 中的任何一个,有没有办法允许两者同时使用,以便 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',
],
不使用架构, 是一个 solution/workaround,需要更改架构,我不喜欢这样。另外我不需要访问令牌来注册,这个答案在做什么。
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');
});
config/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 和网络应用程序同时工作,现在我可以使用其中任何一个。
更新2
正如@Lim Kean Phang 所建议的 git 问题 link
我改了
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 问题,因为找不到任何可能的解决方案来使其工作。
API 路由,你应该使用 chrome/app 的邮递员来测试 API
Route::group(['prefix' => 'auth',namespace =>'App\Http\Controller'], function () {
Route::post('login', 'Auth\AuthController@login')->name('api.login');
Route::group(['middleware' => 'auth:api'], function () {
Route::post('register', 'Auth\AuthController@register')->name('api.register');
Route::post('forgot-password', 'Auth\ForgotPasswordController@forgotPassword')->name('api.forgot-password');
Route::post('logout', 'Auth\AuthController@logout')->name('api.logout');
});
});
Config/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',
'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',
],
],
我将 AuthController
更改为
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
class AuthController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
/**
* Get a JWT via given credentials.
*
* @return \Illuminate\Http\JsonResponse
*/
public function login()
{
$credentials = request(['username', 'password']);
$token = auth()->guard('api')->attempt($credentials);
if (!$token) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
/**
* Log the user out (Invalidate the token).
*
* @return \Illuminate\Http\JsonResponse
*/
public function logout()
{
auth()->guard('api')->logout();
return response()->json(['message' => 'Successfully logged out']);
}
/**
* Refresh a token.
*
* @return \Illuminate\Http\JsonResponse
*/
public function refresh()
{
return $this->respondWithToken(auth()->refresh());
}
/**
* Get the token array structure.
*
* @param string $token
*
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth('api')->factory()->getTTL() * 60,
]);
}
}
并且在 api.php 中将 auth 更改为 jwt.auth 解决了问题。
Route::group([
'middleware' => 'api',
'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('jwt.auth')->post('logout', 'Auth\AuthController@logout')->name('api.logout');
Route::middleware('auth')->post('refresh', 'Auth\AuthController@refresh')->name('api.refresh');
Route::middleware('jwt.auth')->post('me', 'Auth\AuthController@me')->name('api.me');
});
我自己刚遇到这个问题,post 我的回答以防有人觉得有用。
在我的例子中,我需要我的 API 可以被我的网站和外部客户访问。该网站使用 session
守卫,因为浏览器会自动在每个请求中包含任何会话 cookie。其他客户端使用 api
守卫和 token
驱动程序,因为它们不处理 cookie,而是使用用户 table.[=17= 中的 token_id
字段]
// contig/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
现在我可以这样保护我的路线了:
// routes/api.php
Route::group(['middleware' => ['auth:web,api']], function () {
Route::get('/abc', 'MyController@abc');
});
请注意 Laravel 支持开箱即用的 web,api
语法,但未记录。我通过分析 Laravel 的源代码意识到这是可能的。
你不能同时使用两个守卫(web 和 api),所以你必须做的是使用 JWTAuth::attempt 函数生成 jwt,如下所示。
- 在 AuthController 的顶部添加以下代码
use JWTAuth;
- 使用以下代码更新登录功能。
public function login(){
$credentials = request(['email', 'password']);
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return response()->json(['status'=>200,'token'=>$token]);
}
- 在 auth.php 中确保您使用的是网络防护。
这将生成令牌,您将能够使用这两个身份验证守卫。
在构造函数中设置默认驱动程序也有效:
public function __construct() {
auth()->setDefaultDriver('api');
}
如果您已正确配置所有内容,那么调用 Auth::guard()
应该 return Tymon\JWTAuth\JWTGuard
或者,您也可以将驱动程序作为参数传递给 guard
方法,如下所示:
private method guard() {
return Auth::guard('api'); // Tymon\JWTAuth\JWTGuard
}
Laravel 5.7
PHP 7.2.10
目前我可以使用 web 和 api guards 中的任何一个,有没有办法允许两者同时使用,以便 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',
],
不使用架构,
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');
});
config/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 和网络应用程序同时工作,现在我可以使用其中任何一个。
更新2 正如@Lim Kean Phang 所建议的 git 问题 link
我改了
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 问题,因为找不到任何可能的解决方案来使其工作。
API 路由,你应该使用 chrome/app 的邮递员来测试 API
Route::group(['prefix' => 'auth',namespace =>'App\Http\Controller'], function () {
Route::post('login', 'Auth\AuthController@login')->name('api.login');
Route::group(['middleware' => 'auth:api'], function () {
Route::post('register', 'Auth\AuthController@register')->name('api.register');
Route::post('forgot-password', 'Auth\ForgotPasswordController@forgotPassword')->name('api.forgot-password');
Route::post('logout', 'Auth\AuthController@logout')->name('api.logout');
});
});
Config/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',
'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',
],
],
我将 AuthController
更改为
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
class AuthController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
/**
* Get a JWT via given credentials.
*
* @return \Illuminate\Http\JsonResponse
*/
public function login()
{
$credentials = request(['username', 'password']);
$token = auth()->guard('api')->attempt($credentials);
if (!$token) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
/**
* Log the user out (Invalidate the token).
*
* @return \Illuminate\Http\JsonResponse
*/
public function logout()
{
auth()->guard('api')->logout();
return response()->json(['message' => 'Successfully logged out']);
}
/**
* Refresh a token.
*
* @return \Illuminate\Http\JsonResponse
*/
public function refresh()
{
return $this->respondWithToken(auth()->refresh());
}
/**
* Get the token array structure.
*
* @param string $token
*
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth('api')->factory()->getTTL() * 60,
]);
}
}
并且在 api.php 中将 auth 更改为 jwt.auth 解决了问题。
Route::group([
'middleware' => 'api',
'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('jwt.auth')->post('logout', 'Auth\AuthController@logout')->name('api.logout');
Route::middleware('auth')->post('refresh', 'Auth\AuthController@refresh')->name('api.refresh');
Route::middleware('jwt.auth')->post('me', 'Auth\AuthController@me')->name('api.me');
});
我自己刚遇到这个问题,post 我的回答以防有人觉得有用。
在我的例子中,我需要我的 API 可以被我的网站和外部客户访问。该网站使用 session
守卫,因为浏览器会自动在每个请求中包含任何会话 cookie。其他客户端使用 api
守卫和 token
驱动程序,因为它们不处理 cookie,而是使用用户 table.[=17= 中的 token_id
字段]
// contig/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
现在我可以这样保护我的路线了:
// routes/api.php
Route::group(['middleware' => ['auth:web,api']], function () {
Route::get('/abc', 'MyController@abc');
});
请注意 Laravel 支持开箱即用的 web,api
语法,但未记录。我通过分析 Laravel 的源代码意识到这是可能的。
你不能同时使用两个守卫(web 和 api),所以你必须做的是使用 JWTAuth::attempt 函数生成 jwt,如下所示。
- 在 AuthController 的顶部添加以下代码
use JWTAuth;
- 使用以下代码更新登录功能。
public function login(){
$credentials = request(['email', 'password']);
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return response()->json(['status'=>200,'token'=>$token]);
}
- 在 auth.php 中确保您使用的是网络防护。
这将生成令牌,您将能够使用这两个身份验证守卫。
在构造函数中设置默认驱动程序也有效:
public function __construct() {
auth()->setDefaultDriver('api');
}
如果您已正确配置所有内容,那么调用 Auth::guard()
应该 return Tymon\JWTAuth\JWTGuard
或者,您也可以将驱动程序作为参数传递给 guard
方法,如下所示:
private method guard() {
return Auth::guard('api'); // Tymon\JWTAuth\JWTGuard
}