我目前正在开发一个具有 API 的应用程序,我希望可以通过中间件访问该 API,该 API 将检查用户是否使用 Laravel 的默认 Auth 中间件和 Tymone 的基于 JWT.Auth 令牌的中间件进行身份验证,以便请求可以通过任何一种方式进行身份验证。
我可以弄清楚如何拥有一个或另一个但不能同时拥有,我怎么能做到这一点?我想我需要创建一个使用这些现有中间件的自定义中间件?
我正在使用 Laravel 5.1
谢谢
我尝试构建自己的 API。我开始,所以我目前唯一的模型将是“用户”。这是我想如何调用我的 API:
HTTP/POST http://example.com/api/user/ # get all the users
HTTP/POST http://example.com/api/user/1 # get the user with id "1"
HTTP/POST http://example.com/api/user/1/delete # delete the user with id "1"
...
Run Code Online (Sandbox Code Playgroud)
所以我的文件routes/web.php看起来像这样:
<?php
Route::group(['prefix' => 'api'], function() {
Route::group(['prefix' => 'user'], function() {
Route::post('/', 'ApiController@allUsers');
});
});
?>
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为我没有通过Route::resource静态方法,而是通过常规Route::post方法。所以问题是VerifyCsrfToken中间件将触发并尝试检查我的 CSRF 令牌,但由于我希望我的 api 在未来被许多其他建议使用,我更喜欢使用我自己的安全系统(这将是一个公私密钥对,但现在我只想检查我通过 api 分发的数据的完整性,然后我将设置安全算法)。
好消息是 Laravel 非常干净,可以让您在VerifyCSRFToken数组中添加异常 URL,其形状如下:
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
class VerifyCsrfToken extends BaseVerifier
{
/** …Run Code Online (Sandbox Code Playgroud) 我是第一次测试流明。尝试auth中间件会引发错误。我想知道这样的中间件是否是lumen附带的,还是我们需要实现自己的?这是我的路线文件
$app->group(['middleware' => 'auth'], function ($app) {
$app->get('/', ['as' => 'api', 'uses' => 'ApiController@index']);
});
Run Code Online (Sandbox Code Playgroud)
这是尝试访问路线时的错误
ErrorException in Manager.php line 137:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Auth\Guard' does not have a method 'handle'
Run Code Online (Sandbox Code Playgroud) 这是 Laravel 的ValidatePostSize 中的 handle() 方法:
public function handle($request, Closure $next)
{
$max = $this->getPostMaxSize();
if ($max > 0 && $request->server('CONTENT_LENGTH') > $max) {
throw new PostTooLargeException;
}
return $next($request);
}
Run Code Online (Sandbox Code Playgroud)
现在,使用$next($request)为另一个中间件调用此方法。我的理解是handle()方法被转换为$next。我想知道这是如何发生在引擎盖下的。
我是初学者,正在尝试创建一个推荐系统。以下是示例网址
http://dev.lea.com/register?ref=mh2HPLpVSn
Run Code Online (Sandbox Code Playgroud)
我尝试获取推荐字符串。我怎样才能单独获得?是否需要中间件来获取cookie?如果是,那么中间件的代码是什么?我如何在 RegisterController 中获得推荐参考?
这是我的注册控制器。
<?php
namespace Lea\Http\Controllers\Auth;
use DB;
use Lea\User;
use Lea\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Cookie;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after …Run Code Online (Sandbox Code Playgroud) 我不知道 php 7 的语法我实际上是新手我正在尝试传递像 ['admin','user','cmc'] 这样的角色来路由中间件,如下所示我如何正确地做到这一点
这是我的路线
Route::get('/dashboard','HomeController@dashboard')->name('dashboard')->middleware("roles:['admin','user']");
Run Code Online (Sandbox Code Playgroud)
//我如何在其中传递角色数组
//角色中间件
<?php
namespace App\Http\Middleware;
use Closure;
use App\Role;
use Illuminate\Support\Facades\Log;
class Roles
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next,$role)
{
Log::info($role);
if($request->user()==null){
return response('unauthorised Access detected',401);
}
//check for any role passed from array
if($request->user()->hasAnyRole($role)){
return $next($request);
}
return response('unauthorised Access detected',401);
}
}
Run Code Online (Sandbox Code Playgroud)
//用户模型
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as …Run Code Online (Sandbox Code Playgroud) 提到中间件有两种类型:之前和之后。在处理请求之前处理中间件之前和处理请求之后处理中间件之后。
但是可终止中间件的功能与后中间件完全相同。那么这两者的核心区别是什么呢?谢谢。
我的网络应用程序中有一个页面应该阻止登录用户访问。类似于已登录的用户不应访问登录或注册页面。我通过在控制器构造函数中使用来宾中间件来完成此操作。
$this->middleware("guest")->only("page_action"); // like this
Run Code Online (Sandbox Code Playgroud)
在此设置中,如果登录用户尝试访问该页面,他们将被重定向到主页。但我需要显示 404 未找到页面而不是重定向。我怎样才能做到这一点?
简而言之,如何才能使页面仅可供访客访问,并使其对于登录用户来说看起来不存在?
我不知道为什么我最近通过简单地在页面之间导航而不断收到此消息。
对 null 调用成员函数 setCookie()

这就是我的AdminMiddleware
<?php
namespace App\Http\Middleware;
use App\Article;
use Closure, View, Auth ;
use Illuminate\Contracts\Auth\Guard;
class AdminMiddleware
{
/**
* The Guard implementation.
*
* @var Guard
*/
protected $auth;
/**
* Create a new filter instance.
*
* @param Guard $auth
* @return void
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public …Run Code Online (Sandbox Code Playgroud) 我有一个从中提取数据的数组。
BLOCK_IP_LIST = [127.0.0.1,127.0.0.2,127.0.0.3]
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么做。
我使用 Config>app.php 在 BlockIpMiddleware 中使用 .env BLOCK_IP_LIST
Config.app.php代码如下
'block_ip' => env('BLOCK_IP_LIST'),
Run Code Online (Sandbox Code Playgroud)
我的BlockIpMiddleware代码如下
class BlockIpMiddleware
{
public function handle(Request $request, Closure $next)
{
$blockIps = config('app.block_ip');
if (in_array($request->ip(), $blockIps)) {
return response()->json(['message' => "You don't have permission to access this website."]);
}
return $next($request);
}
}
Run Code Online (Sandbox Code Playgroud) php environment-variables laravel laravel-middleware laravel-8
laravel ×7
php ×6
laravel-5 ×3
laravel-5.8 ×2
middleware ×2
api ×1
laravel-5.1 ×1
laravel-5.4 ×1
laravel-5.6 ×1
laravel-8 ×1
lumen ×1
referrals ×1
routes ×1