在Laravel < 5.5
我可以更改此文件app/Exceptions/Handler
以更改未经身份验证的用户重定向网址:
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest(route('login'));
}
但是Laravel 5.5
已将vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php
移至此位置protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['message' => 'Unauthenticated.'], 401)
: redirect()->guest(route('login'));
}
,那么我该如何更改呢?我不想更改供应商目录中的内容,因为它会被作曲家更新覆盖。
C:\Downloads
答案 0 :(得分:56)
但是在Laravel 5.5中,这已经转移到这个位置供应商/ laravel / framework / src / Illuminate / Foundation / Exceptions / Handler.php,那么我现在该怎么改呢?我不想更改供应商目录中的内容,因为它会被作曲家更新覆盖。
只是默认情况下该功能不存在。
你可以像在5.4中那样覆盖它。请确保包含
use Exception;
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;
处理程序文件中的。
例如我的app/Exceptions/Handler.php
看起来有点像这样:
<?php
namespace App\Exceptions;
use Exception;
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
(...) // The dfault file content
/**
* Convert an authentication exception into a response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['message' => 'Unauthenticated.'], 401)
: redirect()->guest(route('authentication.index'));
}
}
答案 1 :(得分:15)
这是我如何解决它。 在渲染函数中,我捕获了异常类。如果它的身份验证异常类我写了我的重定向代码(我将在以前版本的未经验证的函数中编写的代码)。
public function render($request, Exception $exception)
{
$class = get_class($exception);
switch($class) {
case 'Illuminate\Auth\AuthenticationException':
$guard = array_get($exception->guards(), 0);
switch ($guard) {
case 'admin':
$login = 'admin.login';
break;
default:
$login = 'login';
break;
}
return redirect()->route($login);
}
return parent::render($request, $exception);
}
答案 2 :(得分:5)
但是在Laravel 5.5中,这已经移到了这个位置 供应商/ laravel /框架/ src目录/照亮/基金/例外/ Handler.php 那么我现在怎么改呢?我不想改变供应商的东西 目录包含它被作曲家更新覆盖。
我们需要包括 使用Illuminate \ Auth \ AuthenticationException;
然后它就像laravel 5.4
一样答案 3 :(得分:2)
标准异常处理程序使用命名路由。
因此,您只需定义使用该名称的路线。
因此,在您的routes/web.php
文件中,只需添加以下行:
Route::get('mylogin', 'MyLoginController@showLoginForm')->name('login');
name('login')
位为此路由命名,因此未经身份验证的异常将使用此路由。
您不需要打扰制作自己的异常处理程序或修改标准异常处理程序。
样板'auth'代码使用的命名路由可以在vendor/laravel/framework/src/Illuminate/Routing/Router.php
函数的auth()
文件中找到。 (登录,注销,注册,password.request,password.email和password.reset)。在路径文件中使用Route::auth();
行时会添加这些路径。
答案 4 :(得分:1)
对于Laravel 7.x +和6.x。++
这是我在laravel 7项目中解决问题的方式。
文件:App \ Exceptions \ Handler.php
在code
下将其复制到您的app \ Exception \ Handler
<?php
namespace App\Exceptions;
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;
use Illuminate\Support\Arr;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Exception
*/
public function report(Throwable $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
return parent::render($request, $exception);
}
protected function unauthenticated($request, AuthenticationException $exception)
{
// return $request->expectsJson()
// ? response()->json(['message' => $exception->getMessage()], 401)
// : redirect()->guest(route('login'));
if($request->expectsJson()) {
return response()->json(['message' => $exception->getMessage()],401);
}
$guard = Arr::get($exception->guards(), 0);
switch ($guard) {
case 'admin':
$login = 'admin.login';
break;
case 'vendor':
$login = 'vendor.login';
break;
default:
$login = 'login';
break;
}
return redirect()->guest(route($login));
}
}
答案 5 :(得分:0)
只需在路线文件中添加登录路线:
2018/02/02 10:19:10Z: Message: Windows is Ready to use
EC2 instance <i-xxxxxxxxxxxx> ready.
D [WinRM] <{:endpoint=>"http://<ip>:5985/wsman", :user=>"testkitchen", :password=>"xxxxxxx", :transport=>:negotiate, :elevated_username=>"testkitchen", :elevated_password=>"xxxxxxxx", :no_ssl_peer_verification=>true, :disable_sspi=>false, :basic_auth_only=>false}> (Write-Host '[WinRM] Established
')
D [WinRM] opening remote shell on http://<ip>:5985/wsman
D [WinRM] opening remote shell on http://<ip>:5985/wsman
I, [2018-02-01T22:13:18.894201 #2052] INFO -- Kitchen: -----> Starting Kitchen (v1.19.2)
I, [2018-02-01T22:13:32.925614 #2052] INFO -- Kitchen: -----> Creating <default-windows-2012r2>...
E, [2018-02-01T22:29:02.938333 #2052] ERROR -- Kitchen: ------Exception-------
E, [2018-02-01T22:29:02.938333 #2052] ERROR -- Kitchen: Class: Kitchen::ActionFailed
E, [2018-02-01T22:29:02.938333 #2052] ERROR -- Kitchen: Message: 1 actions failed.
>>>>>> Failed to complete #create action: [Unable to parse authorization header. Headers: {"Via"=>"1.1 10.158.11.157 (McAfee Web Gateway 7.6.2.16.0.24166)", "Date"=>"Thu, 01 Feb 2018 16:55:20 GMT", "Content-Type"=>"text/html", "Cache-Control"=>"no-cache", "Content-Length"=>"2663", "Proxy-Connection"=>"Keep-Alive", "Proxy-Authenticate"=>"Basic realm=\"McAfee Web Gateway\""}
Body: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<!-- FileName: index.html
Language: [en]
-->
<!--Head-->
<head>
type="text/javascript" ></script>
<link rel="stylesheet" href="/mwg-internal/gfddsdfd/files/default/stylesheet.css" />
</head>
<!--/Head-->
<tr>
<td class='footData'>
generated <span id="time">2018-02-01 22:25:20</span> by McAfee Web Gateway
<br />
Ruby WinRM Client (2.8.3, ruby 2.4.2)
</td>
</tr>
答案 6 :(得分:0)
Copy this to your app\Exception\Handler
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;
protected function unauthenticated($request, AuthenticationException $exception){
if ($request->expectsJson()) {
return response()->json(['message' => $exception->getMessage()], 401);
}
$guard = array_get($exception->guards(),0);
switch ($guard) {
case 'admin':
return redirect()->guest(route('admin.login'));
break;
default:
return redirect()->guest(route('login'));
break;
}
}
答案 7 :(得分:0)
对于Laravel 7.x +
===========**top add class:**================
use Illuminate\Auth\AuthenticationException;
use Illuminate\Support\Arr;
=======================================
public function render($request, Throwable $exception)
{
if($exception instanceof AuthenticationException){
$guard = Arr::get($exception->guards(), 0);
switch($guard){
case 'admin':
return redirect(route('admin.login'));
break;
default:
return redirect(route('login'));
break;
}
}
return parent::render($request, $exception);
}
答案 8 :(得分:0)
对于Laravel版本7,*
文件:App \ Exceptions \ Handler.php
使用Illuminate \ Support \ Arr; //顶级
将Illuminate \ Foundation \ Exceptions \ Handler用作ExceptionHandler;
公共函数render($ request,Throwable $ exception) {
// for Multi AUth guard
if($exception instanceof AuthenticationException){
$guard = Arr::get($exception->guards(), 0);
switch($guard){
case 'admin':
return redirect(route('admin.login'));
break;
default:
return redirect(route('login'));
break;
}
}
return parent::render($request, $exception);
}
答案 9 :(得分:-1)
除了overriding
之外,您可以直接在Handler.php
对位于vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php
的现有功能未经身份验证的进行更改,以根据警卫重定向到预期路线。
/**
* Convert an authentication exception into a response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
$guard = array_get($exception->guards(),0);
switch ($guard) {
case 'admin':
return $request->expectsJson()
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->guest(route('admin.login'));
break;
default:
return $request->expectsJson()
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->guest(route('login'));
break;
}
}
答案 10 :(得分:-1)
用以下内容替换您的app \ Exceptions \ Handler.php代码。...
slack
答案 11 :(得分:-14)
1.转到vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php
档案。
2.搜索方法名称为unauthenticated
。
3.并更改重定向网址
redirect()->guest(route('login'))
至redirect()->guest(route('api/login')) //whatever you want
。
如果它是API服务,您可以将响应作为JSON返回。