I managed to get it in this way, in the handler.php
(我设法以这种方式在handler.php
得到它)
use LeagueOAuth2ServerExceptionOAuthServerException;
use IlluminateAuthAuthenticationException;
....
public function report(Exception $exception)
{
if ($exception instanceof OAuthServerException || $exception instanceof AuthenticationException) {
if(isset($exception->guards) && isset($exception->guards()[0]) ==='api')
response()->json('Unauthorized', 401) ;
else if ($exception instanceof OAuthServerException)
response()->json('Unauthorized', 401) ;
}
parent::report($exception);
}
then in order to prevent cross origin error on browser added a middleware
as follows NOTE: make middleware secure in production kernal.php
(然后为了防止浏览器上的跨源错误,添加了如下middleware
注意:使中间件在生产环境kernal.php
安全)
protected $middleware = [
....
AppHttpMiddlewareCors::class,
];
cors.php
use Closure;
class Cors
{
/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
->header('Access-Control-Allow-Headers',' Origin, Content-Type, Accept, Authorization, X-Request-With')
->header('Access-Control-Allow-Credentials',' true');
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…