我完全理解我在本地主机上测试它,但我不知道它是否应该返回我的笔记本电脑的真实IP,或者在以下每一种情况下实际上都是一个环回'::1‘.它在生产中的工作方式是不同的,还是我遗漏了一点?
来自控制器的
<?php
namespace SD\UserBundle\Controller;
use FOS\UserBundle\Controller\SecurityController as OriginalController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
class SecurityController extends OriginalController
{
public function loginAction(Request $request)
{
$clientIp = $this->get('request_stack')
->getMasterRequest()
->getClientIp()
;
if($this->get('sd_user.ip_foresight')->isBlackListed($clientIp))
{
return new RedirectResponse(
$this->generateUrl('sd_user_forbidden')
);
}
return parent::loginAction($request);
}
}来自侦听器的
<?php
namespace SD\UserBundle\EventListener;
use Symfony\Component\HttpFoundation\RequestStack;
class IpListener implements EventSubscriberInterface
{
protected $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function clientIp()
{
return $this->requestStack->getMasterRequest()->getClientIp();
}
// ...发布于 2018-05-18 22:19:44
你这样做是对的。让我们看看IP地址从何而来:
Request对象开始执行过程。因此,最终您将得到real客户端IP地址,而不是被一些称为子请求的操作所覆盖的地址。
但是,在使用负载均衡器时,有一个例外,在本例中,只需阅读正式文档:
https://stackoverflow.com/questions/50419568
复制相似问题