【问题标题】:Laravel Edit Auth MiddlewareLaravel 编辑认证中间件
【发布时间】:2016-04-12 05:11:28
【问题描述】:

我正在尝试编辑主要的Authenticate.php 中间件,但是当我添加以下内容时,出现错误

app.app 重定向您的次数过多

我的意图是编辑 auth 中间件以检查用户是否有用户名。这将防止某人退出注册页面,然后直接进入网站的安全部分。

认证中间件:

public function handle($request, Closure $next)
{
    if ($this->auth->guest()) {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('/');
        }
    }

    // This is the modified portion. Check for a username, if one is found, complete 
       the request, otherwise redirect back to the oauth page.

    if (Auth::user()->username)
    {
        return $next($request);  
    }

    return redirect()->route('oauth.oauth')->with('user' , Auth::user()->id);

}

【问题讨论】:

  • 这个的用例是什么?如果您想阻止某人访问网站的安全部分,那么您可以按原样使用中间件 - 它已经检查他们是否是访客,如果是,它将他们重定向到主页(如果需要)。
  • 注册有2个阶段,第一个是邮箱、密码等。第二个是OAuth与外部服务。如果用户在完成 OAuth 之前退出,如果他们尝试再次打开网站的任何部分,我想将他们送回该页面。由于有几十个路由使用 auth 中间件,我想对其进行修改,而不是制作一个新的以应用到任何地方。
  • 该路由是否受此身份验证中间件保护?
  • 是的,受此中间件保护。

标签: laravel laravel-5.2


【解决方案1】:

Auth::user()->username 不存在时,您通过使用 auth 中间件将它们重定向到您的 oauth 页面来创建循环引用。

他们正在访问 oauth 页面,然后检查失败,因此不断被重定向到该页面。

最好的做法是将其拆分到新的中间件中,但鉴于您不想这样做,您可以检查他们点击的 URL 并根据此排除。

例如:

在顶部添加use Request;,然后在中间件主体中添加以下内容:

if (Request::path() == 'your/oauth/path')
{
    return $next($request);
}

所以它可以像这样适合:

public function handle($request, Closure $next)
{
    if ($this->auth->guest()) {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('/');
        }
    }

    // This is the modified portion. Check for a username, if one is found, complete 
       the request, otherwise redirect back to the oauth page.

    if (Auth::user()->username)
    {
        return $next($request);  
    }

    if (Request::path() == 'your/oauth/path')
    {
        return $next($request);
    }

    return redirect()->route('oauth.oauth')->with('user' , Auth::user()->id);

}

只需将'your/oauth/path' 替换为实际路径即可。该示例看起来像一个完整的 url www.example.com/your/oauth/path

【讨论】:

    猜你喜欢
    • 2017-12-30
    • 2016-04-27
    • 2021-03-24
    • 2017-06-27
    • 2020-11-09
    • 1970-01-01
    • 2020-07-25
    • 2018-11-07
    • 2019-03-10
    相关资源
    最近更新 更多