gpt4 book ai didi

php - Symfony2 : how to check if an action is secured?

转载 作者:可可西里 更新时间:2023-11-01 12:31:00 24 4
gpt4 key购买 nike

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

我的用例如下:

  • 用户可以注册并登录
  • 如果他登录并尝试访问安全页面,他将被重定向到“测试版”页面,直到 6 月底。
  • 如果他尝试访问普通页面(不安全),他将能够在没有任何重定向的情况下访问该页面。

感谢您的帮助!

奥雷尔

最佳答案

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

创建一个Token类,

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_login 监听器,它使用给定的 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;
}

创建一个入口点类。监听器将从此类创建一个 RedirectResponse

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 DependencyInjection 文件夹中创建一个名为 Security/Factory 的文件夹。然后创建工厂类。

namespace Your\Bundle\DependencyInjection\Security\Factory;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;

class MyFirewallFactory implements SecurityFactoryInterface
{

public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
{
$provider = 'my_firewall.auth_provider.'.$id;
$container->setDefinition($provider, new DefinitionDecorator('my_firewall.auth_provider'));

// entry point
$entryPointId = $this->createEntryPoint($container, $id, $config, $defaultEntryPoint);

// listener
$listenerId = 'my_firewall.security.authentication.listener'.$id;
$listener = $container->setDefinition($listenerId, new DefinitionDecorator('my_firewall.security.authentication.listener'));
$listener->replaceArgument(1, new Reference($entryPointId));
return array($provider, $listenerId, $entryPointId);
}

public function getPosition()
{
return 'pre_auth';
}

public function getKey()
{
return 'my_firewall'; //the listener name
}

protected function getListenerId()
{
return 'my_firewall.security.authentication.listener';
}

public function addConfiguration(NodeDefinition $node)
{
$node
->children()
->scalarNode('redirect_path')->end()
->end()
;
}

protected function createEntryPoint($container, $id, $config, $defaultEntryPointId)
{
$entryPointId = 'my_firewall.entry_point'.$id;
$container
->setDefinition($entryPointId, new DefinitionDecorator('my_firewall.entry_point'))
->addArgument(new Reference('security.http_utils'))
->addArgument($config['redirect_path'])
;
return $entryPointId;
}

}

然后在您的捆绑文件夹的 NamespaceBundle.php 中添加以下代码。

public function build(ContainerBuilder $builder){
parent::build($builder);
$extension = $builder->getExtension('security');
$extension->addSecurityListenerFactory(new Security\Factory\MyFirewallFactory());
}

身份验证监听器已创建,呸:)。现在在您的 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

关于php - Symfony2 : how to check if an action is secured?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10089816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com