Laravel如何捕获419错误然后重定向到不同的登录页面。

huangapple go评论68阅读模式
英文:

Laravel How to capture 419 then redirect to different login page

问题

Here is the translated content:

我已经搜索了很多。大多数帖子都说,设置csrf,
1 meta标签,name="csrf-token",content="{{ csrf_token() }}"
2 @csrf在表单标签内
3 排除路由

另一个解决方法是延长时间。

Route::get('refresh-token', function(){
    return csrf_token();
});

然后使用JavaScript编写setInterval以获取新的csrf...

我已经尝试了这些。但我想要更好、更简单的解决方案:

**如果出现419错误,重定向到登录页面。**
前端跳转至 http://mysite/login
管理员跳转至 http://mysite/admin/login

**如何捕获419和当前部分(是否为管理员)然后重定向?**

ChatGPT建议我使用App\Exceptions\Handler。它说使用render(),但在Laravel 10中有register()。我认为函数内部的代码是相同的。

它使用:

public function render($request, Exception $exception)

我的Laravel是

public function register()

我更改为

public function register(Request $request)

它说

'App\Exceptions\Handler::register()' 与方法 'Illuminate\Foundation\Exceptions\Handler::register()' 不兼容

我使用

public function register()
{
    $this->reportable(function (Throwable $e) {
        //
    });

    $request = new Request;
    $segment = $request->segment();
    echo '<pre>', print_r($segment, 1), "</pre>"; exit;

    $this->renderable(function (\Exception $e) {
        if ($e->getPrevious() instanceof \Illuminate\Session\TokenMismatchException) {
            return redirect()->route('lang.admin.login');
        };
    });
}

我认为我找到了解决方案。在renderable()中添加$request:

public function register()
{
    $this->reportable(function (Throwable $e) {
        //
    });

    $this->renderable(function (\Exception $e, $request) {
        $segments = $request->segments();

        if ($e->getPrevious() instanceof \Illuminate\Session\TokenMismatchException) {
            if($segments[1] == 'admin'){
                return redirect()->route('admin.login');
            }else{
                return redirect()->route('login');
            }
        };
    });
}

Hope this helps! If you have any more questions or need further assistance, feel free to ask.

英文:

I have googled a lot. Most of the posts says, set csrf,
1 meta tag, name="csrf-token", content="{{ csrf_token() }}"
2 @csrf inside form tag
3 exclude the route

Another workround, to extend the time.

Route::get(&#39;refresh-token&#39;, function(){
    return csrf_token();
});

Then use javascript write setInterval to get new csrf...

These I have did. But I want better and more simple solution:

If 419 happens, redirect to login page.
Frontend to http://mysite/login
Admin to http://mysite/admin/login

How to capture 419 and the current section (admin or not) then redirect??

ChatGPT tells me to use App\Exceptions\Handler. It says render(), but there is register() in laravel10. I think the code is the same inside the function.

It use:

public function render($request, Exception $exception)

My laravel was

public function register()

I change to

public function register(Request $request)

It says

&#39;App\Exceptions\Handler::register()&#39; is not compatible with method &#39;Illuminate\Foundation\Exceptions\Handler::register()

I use

public function register()
{
    $this-&gt;reportable(function (Throwable $e) {
        //
    });

    $request = new Request;
    $segment = $request-&gt;segment();
    echo &#39;&lt;pre&gt;&#39;, print_r($segment, 1), &quot;&lt;/pre&gt;&quot;; exit;

    $this-&gt;renderable(function (\Exception $e) {
        if ($e-&gt;getPrevious() instanceof \Illuminate\Session\TokenMismatchException) {
            return redirect()-&gt;route(&#39;lang.admin.login&#39;);
        };
    });
}

It give me white page.


I think I found the solution. Add $request inside renderable():

public function register()
{
    $this-&gt;reportable(function (Throwable $e) {
        //
    });

    $this-&gt;renderable(function (\Exception $e, $request) {
        $segments = $request-&gt;segments();

        if ($e-&gt;getPrevious() instanceof \Illuminate\Session\TokenMismatchException) {
            if($segments[1] == &#39;admin&#39;){
                return redirect()-&gt;route(&#39;admin.login&#39;);
            }else{
                return redirect()-&gt;route(&#39;login&#39;);
            }
        };
    });
}

答案1

得分: 1

如何捕获419错误:使用 app\Exceptions\Handler.php
如何获取当前URL段:使用 $request

app\Exceptions\Handler.php

use Illuminate\Http\Request;
...

public function register()
{
    $this->reportable(function (Throwable $e) {
        //
    });

    $this->renderable(function (\Exception $e, $request) {
        $segments = $request->segments();

        if ($e->getPrevious() instanceof \Illuminate\Session\TokenMismatchException) {
            if($segments[1] == 'admin'){
                return redirect()->route('admin.login');
            }else{
                return redirect()->route('login');
            }
        };
    });
}

无需定义 $request,只需将其放在 renderable(function (\Exception $e, $request)) 中。

英文:

How to capture 419: use app\Exceptions\Handler.php
How to get current url segment: use $request

app\Exceptions\Handler.php

use Illuminate\Http\Request;
...

public function register()
{
    $this-&gt;reportable(function (Throwable $e) {
        //
    });

    $this-&gt;renderable(function (\Exception $e, $request) {
        $segments = $request-&gt;segments();

        if ($e-&gt;getPrevious() instanceof \Illuminate\Session\TokenMismatchException) {
            if($segments[1] == &#39;admin&#39;){
                return redirect()-&gt;route(&#39;admin.login&#39;);
            }else{
                return redirect()-&gt;route(&#39;login&#39;);
            }
        };
    });
}

We don't need to define $request, just put it inside
renderable(function (\Exception $e, $request))

答案2

得分: 0

尝试在renderable回调中处理TokenMismatchException错误,如下所示:

$this->renderable(function (TokenMismatchException $e, $request) {
    $segments = $request->segments();
    if(isset($segments[1]) && $segments[1] == 'admin'){
        return redirect()->route('admin.login');
    }else{
        return redirect()->route('login');
    }
});

如果需要进一步帮助,请随时提问。

英文:

Try the TokenMismatchException error in renderable callback like this

$this-&gt;renderable(function (TokenMismatchException $e, $request) {
    $segments = $request-&gt;segments();
    if(isset($segments[1]) &amp;&amp; $segments[1] == &#39;admin&#39;){
        return redirect()-&gt;route(&#39;admin.login&#39;);
    }else{
        return redirect()-&gt;route(&#39;login&#39;);
    }
});

huangapple
  • 本文由 发表于 2023年6月6日 10:24:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定