当用户在 Laravel 中未被授权时返回响应
Returning response when user is unauthorised in Laravel
当用户未通过身份验证时,Laravel 将使用 auth::api
中间件对路由发出的所有请求重定向到默认的 laravel 登录页面。但是,由于我只构建 API,我想 return 对任何将消耗我的 api 的应用程序做出 401 unauthorised
响应。这应该很简单,但出于某种原因,我还不能做到这一点。这是我的代码的样子
public function show(User $user)
{
if ($user->id == auth()->user()->id) {
// do something here
} else {
return response()->json([ 'status_message' => 'Unauthorised'], 401);
}
}
public function update(Request $request, User $user)
{
if ($user->id == auth()->user()->id) {
// do something here
} else {
return response()->json(['status_message' => 'Unathorised'], 401);
}
}
当我访问调用这些方法的端点并且用户未通过身份验证时,我被重定向到默认的 laravel 登录页面。如何覆盖此默认行为?
在你的路由中调用api.auth中间件如下
$api->group(['middleware' => 'api.auth'], function ($api){
//your routes
});
在 Middleware 文件夹中创建文件 Authenticate.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;
class Authenticate {
/**
* The authentication guard factory instance.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Auth\Factory $auth
* @return void
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if ($this->auth->guard($guard)->guest()) {
return response('Unauthorized.', 401);
}
return $next($request);
}
}
并且在 http 文件夹内的 kernel.php 中包含以下内容
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
];
问题解决了伙计们。这是我的做法,在 app\Exceptions\
的 Handle.php
文件中,我将此代码添加到渲染函数
if ($exception instanceof AuthenticationException) {
return response()->json(['status_message' => 'Unauthorised'], 401);
}
就是这样。
注意:这在 laravel 5.8
中有效
当用户未通过身份验证时,Laravel 将使用 auth::api
中间件对路由发出的所有请求重定向到默认的 laravel 登录页面。但是,由于我只构建 API,我想 return 对任何将消耗我的 api 的应用程序做出 401 unauthorised
响应。这应该很简单,但出于某种原因,我还不能做到这一点。这是我的代码的样子
public function show(User $user)
{
if ($user->id == auth()->user()->id) {
// do something here
} else {
return response()->json([ 'status_message' => 'Unauthorised'], 401);
}
}
public function update(Request $request, User $user)
{
if ($user->id == auth()->user()->id) {
// do something here
} else {
return response()->json(['status_message' => 'Unathorised'], 401);
}
}
当我访问调用这些方法的端点并且用户未通过身份验证时,我被重定向到默认的 laravel 登录页面。如何覆盖此默认行为?
在你的路由中调用api.auth中间件如下
$api->group(['middleware' => 'api.auth'], function ($api){
//your routes
});
在 Middleware 文件夹中创建文件 Authenticate.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;
class Authenticate {
/**
* The authentication guard factory instance.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Auth\Factory $auth
* @return void
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if ($this->auth->guard($guard)->guest()) {
return response('Unauthorized.', 401);
}
return $next($request);
}
}
并且在 http 文件夹内的 kernel.php 中包含以下内容
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
];
问题解决了伙计们。这是我的做法,在 app\Exceptions\
的 Handle.php
文件中,我将此代码添加到渲染函数
if ($exception instanceof AuthenticationException) {
return response()->json(['status_message' => 'Unauthorised'], 401);
}
就是这样。
注意:这在 laravel 5.8
中有效