我正在Symfony3项目中使用Scheb的Symfony3,我想要处理与它不同的exclude_pattern参数,但我不知道如何处理。
通常,unauthenticated用于将exclude_pattern路由排除在双因素身份验证之外,例如调试页或静态内容:
# config/config.yml
scheb_two_factor:
...
exclude_pattern: ^/(_(profiler|wdt)|css|images|js)/它的行为实现如下:
/* vendor/scheb/two-factor-bundle/Security/TwoFactor/EventListener/RequestListener.php */
public function onCoreRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
// Exclude path
if ($this->excludePattern !== null && preg_match('#'.$this->excludePattern.'#', $request->getPathInfo())) {
return;
}
...
}我还想为认证的路由处理,以便在调用它们时可以跳过双因素身份验证。对于身份验证,我指的是access_control节中的security.yml,如下所示:
# app/config/security.yml
security:
...
access_control:
- { path: ^/test, role: ROLE_USER }现在,如果我在exclude_pattern下添加了一个经过身份验证的路由,我所得到的就是一个AccessDeniedException,可能是因为包要求将access_decision_manager参数设置为strategy: unanimous。
目的是长时间讲,英语不是我的母语,但如果你真的需要知道它,我可以尝试解释。
我用symfony3和symfony2标记了这个问题,因为我使用的是Symfony3.0,但我很确定它在Symfony2.8中是相同的。
发布于 2016-04-06 15:38:09
通过从包中重写选民类,我找到了一个解决方案:
// AppBundle/Security/TwoFactor/Voter.php
namespace AppBundle\Security\TwoFactor;
use Scheb\TwoFactorBundle\Security\TwoFactor\Session\SessionFlagManager;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class Voter extends \Scheb\TwoFactorBundle\Security\TwoFactor\Voter
{
/**
* @var string
*/
protected $excludePattern;
/**
* Voter constructor.
* @param SessionFlagManager $sessionFlagManager
* @param array $providers
* @param $excludePattern
*/
public function __construct(SessionFlagManager $sessionFlagManager, array $providers, $excludePattern)
{
parent::__construct($sessionFlagManager, $providers);
$this->excludePattern = $excludePattern;
}
/**
* @param TokenInterface $token
* @param mixed $object
* @param array $attributes
*
* @return mixed result
*/
public function vote(TokenInterface $token, $object, array $attributes)
{
if ($this->excludePattern !== null && preg_match('#'.$this->excludePattern.'#', $object->getPathInfo()))
{
return true;
}
parent::vote($token, $object, $attributes);
}
}# app/config/services.yml
services:
...
scheb_two_factor.security_voter:
class: 'AppBundle\Security\TwoFactor\Voter'
arguments:
- '@scheb_two_factor.session_flag_manager'
- ~
- '%scheb_two_factor.exclude_pattern%'这样,每当触发GetResponseEvent时,将调用正确的投票者,如果exclude_pattern与路径匹配,则该选民将投票给exclude_pattern。
https://stackoverflow.com/questions/36453147
复制相似问题