如果客户端IP地址匹配IP地址192.168.1.154,我想通过登录页面自动重定向到仪表板页面。但是在我的仪表板页面中,我已经通过Auth
进行了会话我的内核中间件:
{% extends base.html %}
{% load static %}
{% block body %}
<h1> Hello, World! </h1>
{% endblock body %}
{% block javascript %}
<script src="{% static "app1/bootstrap/js/somerandom.js" %}">
var variable = {{ my_var }};
</script>
{% endblock javascript %}
我的IpMiddleware代码:
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'ipcheck' => \App\Http\Middleware\IpMiddleware::class,
];
我的路线登录:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class IpMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// $ip = \Request::ip();
if ($request->ip() == "192.168.1.154") {
return redirect('home');
}
return $next($request);
}
}
我的路线回家:
Route::get('/', ['middleware' => ['ipcheck'], function () {
return view('auth.login');
}]);
我收到了一个错误:
Route::get('home', function () {
if (Auth::guest()) {
return back()->withInput();
} else (Auth::user()->role_id == 1) {
return view('dashboard');
}
});
我现在能做什么?
答案 0 :(得分:3)
您需要在middleware
而不是dashboard
如果你想要身份1
的自动登录,那么它就应该是这样的
public function handle($request, Closure $next)
{
// $ip = \Request::ip();
if ($request->ip() == "192.168.1.154") {
$user_id = 1;//
Auth::loginUsingId($user_id);
return redirect('home');
}
return $next($request);
}
在home
路由中,您的支票Auth
对于未登录用户的访客是真的,因此它会再次重定向到auth.login
,因为auth.login
有中间件重定向到{{ 1}}然后home
检查home
或guest
.....及其制作内容并且您收到错误
此网页有重定向循环
ERR_TOO_MANY_REDIRECTS