gpt4 book ai didi

laravel - 如何在 laravel 中同时启用 api 和 web 防护

转载 作者:行者123 更新时间:2023-12-02 11:05:12 28 4
gpt4 key购买 nike

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是一种需要更改模式的解决方案/解决方法,我不喜欢这样做。另外,我不需要访问 token 来注册,这个答案正在做什么。

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 值,但现在我没有获取访问 token 。

API 响应是

{
"access_token": true,
"token_type": "bearer",
"expires_in": 31536000,
"status": 200,
"response": "Successfully login"
}

更新3添加了github问题,因为无法找到任何可能的解决方案来使其工作。

最佳答案

我将 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');
});

关于laravel - 如何在 laravel 中同时启用 api 和 web 防护,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54477420/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com