基于this我在我的应用中添加了相同的中间件,并带有重定向循环的结果。
如果用户未经过身份验证,应用会重定向到登录页面,因此对我来说,这就是问题所在。或许不是。
ssl证书安装正确并且有效(如果我手动转到https://myapp.org它可以按预期工作)。
主要问题是我在应用程序中使用帮助器url()来生成URL,现在我需要重定向到安全URL。
我尝试删除该中间件,并将重定向添加到.htaccess但结果是相同的(此网页有重定向循环)。
UPDATE 我试图调试问题,看起来没有重定向到https,这就是重定向循环的原因。我为标题添加了日志....
{
"host": [
"myapp.org"
],
"accept": [
"text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,*\/*;q=0.8"
],
"user-agent": [
"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/41.0.2272.76 Safari\/537.36"
],
"accept-encoding": [
"gzip, deflate, sdch"
],
"accept-language": [
"en-US,en;q=0.8"
],
"cookie": [
"XSRF-TOKEN=..."
],
"x-forwarded-for": [
"IP ADDRESS"
],
"x-forwarded-host": [
"myapp.org"
],
"x-forwarded-server": [
"ip-IP.compute.internal"
],
"connection": [
"Keep-Alive"
]
}
我也记录了$request->secure()
的值,它总是错误的。
这是我的中间件(日志的结果在每个日志行旁边的注释中)
<?php namespace App\Http\Middleware;
use Closure;
use Log;
class HttpsProtocol
{
/**
* Redirect to https.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
if(!$request->secure() && app()->environment() === 'production') {
Log::info(app()->environment()); // "production"
Log::info($request->secure()); // ""
Log::info($request->header('x-forwarded-proto')); // ""
Log::info(json_encode($request->header())); // The json just posted
Log::info($request->getRequestUri()); // "/auth/login"
return redirect()->secure($request->getRequestUri());
}
return $next($request);
}
}
我的应用程序位于Elastic Beanstalk(EC2单实例)中。 任何帮助将不胜感激,谢谢。