Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2:如何检查操作是否安全?_Php_Security_Symfony - Fatal编程技术网

Php Symfony2:如何检查操作是否安全?

Php Symfony2:如何检查操作是否安全?,php,security,symfony,Php,Security,Symfony,我想从控制器内部检查它是否是安全页面。 如何做到这一点 我的用例如下所示: 用户可以注册和登录 如果他登录并试图访问安全页面,他将被重定向到“测试版”页面,直到6月底 如果他试图访问一个正常的页面(不安全),他将能够访问它没有任何重定向 谢谢你的帮助 奥雷尔我不知道这是不是正确的方法 但是你可以试试下面的方法 /vendor/symfony/src/symfony/Component/HttpKernel/HttpKernel.php有一个将请求转换为响应的方法handleRaw。请求对象可

我想从控制器内部检查它是否是安全页面。 如何做到这一点

我的用例如下所示:

  • 用户可以注册和登录
  • 如果他登录并试图访问安全页面,他将被重定向到“测试版”页面,直到6月底
  • 如果他试图访问一个正常的页面(不安全),他将能够访问它没有任何重定向
谢谢你的帮助


奥雷尔我不知道这是不是正确的方法 但是你可以试试下面的方法

/vendor/symfony/src/symfony/Component/HttpKernel/HttpKernel.php有一个将请求转换为响应的方法handleRaw。请求对象可以从那里访问。您可以检查客户端是否已请求访问受保护的页面。如果是,您可以手动设置控制器,如

$request->attributes->set('_controller','your\Bundle\SecureBundle\Controller\SecureController::secureAction'); 
另一种解决方案是,如果用户试图访问安全页面并在控制器内检查该页面,则设置会话


同样,这可能不是正确的方法,但它是一种可能的解决方法

当Symfony2处理请求时,它将url模式与
app/config/security.yml
中定义的每个防火墙相匹配。当url模式与防火墙模式匹配时,Symfony2创建一些侦听器对象,并调用这些对象的
handle
方法。如果任何侦听器返回一个
响应
对象,则循环中断,Symfony2输出响应。身份验证部分在身份验证侦听器中完成。它们是根据匹配防火墙中定义的配置创建的,例如
form\u login
http\u basic
等。如果用户未经身份验证,则身份验证的侦听器将创建一个
RedirectResponse
对象以将用户重定向到登录页面。对于您的情况,您可以通过创建自定义身份验证侦听器并将其添加到安全页面防火墙中进行欺骗。示例实现如下所示:

创建一个
令牌

namespace Your\Namespace;

use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;

class MyToken extends AbstractToken
{
    public function __construct(array $roles = array())
    {
        parent::__construct($roles);
    }

    public function getCredentials()
    {
        return '';
    }
}
创建一个实现
AuthenticationProviderInterface
的类。对于
form\u登录
侦听器,它使用给定的
UserProvider
进行身份验证。在这种情况下,它将无所作为

namespace Your\Namespace;

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
use Acme\BaseBundle\Firewall\MyToken;

class MyAuthProvider implements AuthenticationProviderInterface
{

    public function authenticate(TokenInterface $token)
    {
        if (!$this->supports($token)) {
            return null;
        }

        throw new \Exception('you should not get here');
    }

    public function supports(TokenInterface $token)
    {
        return $token instanceof MyToken;
    }
创建一个入口点类。侦听器将从此类创建一个
重定向响应

namespace Your\Namespace;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
use Symfony\Component\Security\Http\HttpUtils;


class MyAuthenticationEntryPoint implements AuthenticationEntryPointInterface
{
    private $httpUtils;
    private $redirectPath;

    public function __construct(HttpUtils $httpUtils, $redirectPath)
    {
        $this->httpUtils = $httpUtils;
        $this->redirectPath = $redirectPath;
    }

    /**
     * {@inheritdoc}
     */
    public function start(Request $request, AuthenticationException $authException = null)
    {
        //redirect action goes here
        return $this->httpUtils->createRedirectResponse($request, $this->redirectPath);
    }
创建一个侦听器类。在这里,您将实现重定向逻辑

namespace Your\Namespace;

use Symfony\Component\Security\Http\Firewall\ListenerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;

class MyAuthenticationListener implements ListenerInterface
{
    private $securityContext;
    private $authenticationEntryPoint;


    public function __construct(SecurityContextInterface $securityContext, AuthenticationEntryPointInterface $authenticationEntryPoint)
    {
        $this->securityContext = $securityContext;
        $this->authenticationEntryPoint = $authenticationEntryPoint;
    }

    public function handle(GetResponseEvent $event)
    {
        $token = $this->securityContext->getToken();
        $request = $event->getRequest();
        if($token === null){
            return;
        }

        //add your logic
        $redirect = // boolean value based on your logic

        if($token->isAuthenticated() && $redirect){

            $response = $this->authenticationEntryPoint->start($request);
            $event->setResponse($response);
            return;
        }
    }

}
创建服务

<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>

        <service id="my_firewall.security.authentication.listener"
                 class="Your\Namespace\MyAuthenticationListener"
                 parent="security.authentication.listener.abstract"
                 abstract="true">
            <argument type="service" id="security.context" />
            <argument /> <!-- Entry Point -->
        </service>

        <service id="my_firewall.entry_point" class="Your\Namespace\MyAuthenticationEntryPoint" public="false" ></service>

        <service id="my_firewall.auth_provider" class="Your\Namespace\MyAuthProvider" public="false"></service>
    </services>

</container>
然后在bundle文件夹的
NamespaceBundle.php
中添加以下代码

public function build(ContainerBuilder $builder){
    parent::build($builder);
    $extension = $builder->getExtension('security');
    $extension->addSecurityListenerFactory(new Security\Factory\MyFirewallFactory());
}
已创建身份验证侦听器,phew:)。现在在你的
app/config/security.yml中执行以下操作

api_area:
  pattern: ^/secured/
  provider: fos_userbundle
  form_login:
    check_path: /login_check
    login_path: /login
    csrf_provider: form.csrf_provider
  my_firewall:
    redirect_path: /beta
  logout: true
  anonymous: true

我不确定你想达到什么目的。当用户键入URL(或被重定向到)操作时,该操作是安全的还是不安全的。显然,由于方法名的原因,控制器不能有多个名称相同的操作(安全操作和非安全操作)。但是,您可以询问SecureContext访问用户是否具有相应的角色,然后对请求执行某些操作(例如转发、重定向)
api_area:
  pattern: ^/secured/
  provider: fos_userbundle
  form_login:
    check_path: /login_check
    login_path: /login
    csrf_provider: form.csrf_provider
  my_firewall:
    redirect_path: /beta
  logout: true
  anonymous: true